Basic Python List help
#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.
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 accountSign in
Already have an account? Sign in here.
Sign In Now