Jump to content

Basic Python List help

Guest
Go to solution Solved by MikeD,
#countries.pycon = [('uk', 'united kingdom'), ('us', 'usa', 'america', 'united states of america'), ('japan')]accepted = [ ]while len(accepted) < 196:    print("You have ", len(accepted), "/ 196 Countries!")    uInput = input("Enter the country: ")    print("")    if uInput.lower() in con:        if uInput.lower() in accepted:            print("Already got that one!")            print("")        else:            accepted.append(uInput.lower())            print("Nice! Now for the next!")            print("")    else:        print("Country not recognised, did you spell it right?")        print("")print("You got them all!")

Right ok.... Don't quite get what that all means... so why doesn't this work?

 

 

Because you are trying to search for a string in a list of tuples. What you want is to search for that string inside the tuples in the list.

 

To clarify a bit, the line

ans = [True if 'com' in x else False for x in listy]

is syntactic sugar (a simpler syntax to do the same thing) for a map with a lambda.

You can find some more information here: http://docs.python.org/2/tutorial/datastructures.html

 

Basically, ans will be a list that results from applying the if statement "True if 'com' in x else False" to every element x of listy.

That means that it will go through the elements of listy and append to the result (list ans) True if the element you are looking for (uInput in your case) is in that tuple or false otherwise.

Then, it's just a matter of finding out if there was a match (True is in the resulting list) and where it is (it's position inside the list will be the same as the position of the tuple that contained the string in the original list).

 

Hope that (somewhat) clears it up a bit.

So me and a mate are creating a simple game where the user has to enter the country, so by the end it there would be around 196 countries, but some of those have multiple names, e.g. UK and United Kingdom. Now the way I have coded this so far, every answer gets checked against the list of countries, if it's there it'll check a second list made up from previously correct inputs, then if if its not in there it'll add it and bam, you have another point. So my issue is the multiple named countries like the UK. Whats the best way to go about checking for multiple names?

 

So far I have decided to use .readlines() on a separate .txt file, laid out below. However, it recognizes the first value, uk and nothing else, like japan or us. Also how do I then eliminate the country from being used again? Otherwise UK and United Kingdom could be used to rack up extra points...

 

.txt file \/

uk, united kingdom, great britan, gbus, america, united states of america, usajapan

My code so far (Bare in mind, this is it so far, I was using an in code list like: con = [ "uk" , "us" , "japan" ] and then the value used would be added to the accepted varible)

#countries.pycon = open("country_list.txt" , "r")print(con.readlines(2))accepted = [ ]while len(accepted) < 196:    print("You have ", len(accepted), "/ 196 Countries!")    uInput = input("Enter the country: ")    print("")    if uInput.lower() in con.readlines(4):        if uInput.lower() in accepted:            print("Already got that one!")            print("")        else:            accepted.append(uInput.lower())            print("Nice! Now for the next!")            print("")    else:        print("Country not recognised, did you spell it right?")        print("")print("You got them all!")
Link to comment
Share on other sites

Link to post
Share on other sites

You keep using readlines(). I don't think it means what you think it means!

 

readlines will read the file and advance the cursor, so eventually you will reach the end of the file and it will stop returning lines.

I use it without arguments, but apparently the optional argument is the least amount of bytes that should be read. It will always read until and end of line or file.

 

What you want to do is read the whole file to a variable just once with conn.readilnes() and there you'll have the entire contents.

 

As for the multiple options that represent the same country I just learned something new in python in just 5 minutes.
If you have, after reading the file, a list of tuples or lists, you can use the function map to check if an element is in one of the tuples of the list.

Here is the example I just made:

>>> listy = [('a'), ('b', 'ba', 'bc'), ('c', 'com')]>>> ans = map(lambda x: True if 'com' in x else False, listy)>>> True if True in ans else FalseTrue>>> ans = map(lambda x: True if 'banana' in x else False, listy)>>> True if True in ans else FalseFalse

It's not exactly the best code ever, but shows that it works. The >>> are from the interpreter.

 

If you want to know which index the answer is in you can do

>>> ans = [True if 'com' in x else False for x in listy]>>> if True in ans:...   print(ans.index(True))... else:...   print('nope')...2>>>
Link to comment
Share on other sites

Link to post
Share on other sites

#countries.pycon = [('uk', 'united kingdom'), ('us', 'usa', 'america', 'united states of america'), ('japan')]accepted = [ ]while len(accepted) < 196:    print("You have ", len(accepted), "/ 196 Countries!")    uInput = input("Enter the country: ")    print("")    if uInput.lower() in con:        if uInput.lower() in accepted:            print("Already got that one!")            print("")        else:            accepted.append(uInput.lower())            print("Nice! Now for the next!")            print("")    else:        print("Country not recognised, did you spell it right?")        print("")print("You got them all!")

Right ok.... Don't quite get what that all means... so why doesn't this work?

Link to comment
Share on other sites

Link to post
Share on other sites

#countries.pycon = [('uk', 'united kingdom'), ('us', 'usa', 'america', 'united states of america'), ('japan')]accepted = [ ]while len(accepted) < 196:    print("You have ", len(accepted), "/ 196 Countries!")    uInput = input("Enter the country: ")    print("")    if uInput.lower() in con:        if uInput.lower() in accepted:            print("Already got that one!")            print("")        else:            accepted.append(uInput.lower())            print("Nice! Now for the next!")            print("")    else:        print("Country not recognised, did you spell it right?")        print("")print("You got them all!")

Right ok.... Don't quite get what that all means... so why doesn't this work?

 

 

Because you are trying to search for a string in a list of tuples. What you want is to search for that string inside the tuples in the list.

 

To clarify a bit, the line

ans = [True if 'com' in x else False for x in listy]

is syntactic sugar (a simpler syntax to do the same thing) for a map with a lambda.

You can find some more information here: http://docs.python.org/2/tutorial/datastructures.html

 

Basically, ans will be a list that results from applying the if statement "True if 'com' in x else False" to every element x of listy.

That means that it will go through the elements of listy and append to the result (list ans) True if the element you are looking for (uInput in your case) is in that tuple or false otherwise.

Then, it's just a matter of finding out if there was a match (True is in the resulting list) and where it is (it's position inside the list will be the same as the position of the tuple that contained the string in the original list).

 

Hope that (somewhat) clears it up a bit.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok then... so do I implent this into my own code? Im a bit thick haha, cant quite get my head around it :/

 

*Edit* All sorted now!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×