Jam3sn
Jam3sn

Reputation: 1087

Python Lists Issues

So I'm new to coding in general but I'm learning Python. So I've put this together but its not comparing the uInput to the 'con' list. What am I doing wrong here?

#countries.py
con = [('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!")

*Edited

So this is my code now updated, but its not check duplicates or adding them, you can enter UK as many times as you wish. But because nothings being added to the accepted list, the points don't increase either.

#countries.py
con = [('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("")
    foundCon = False
    for conTuple in con:
        if uInput.lower() in conTuple:
            foundCon = True
            print("Nice! Now for the next!")
            print("")
            ...
        duplicate = False
        for c in accepted:
            if c in conTuple:
                duplicate = true
        if duplicate:
            print("You've already entered that one!")
            ...
    if not foundCon:
        print("Country not recognised, did you spell it right?")
        print("")
print("You got them all!")
...

Upvotes: 2

Views: 98

Answers (3)

flakes
flakes

Reputation: 23624

when you call input you return a string, meaning that uInput is a string.

Your list con is a list of tuples.. So in the line:

if uInput.lower() in con:

you're always going to get false because a string is being compared to a tuple. It is not being compared to every element in each tuple, just the tuple as a whole. Thus uInput will never be equal to an element of con.

Perhaps try changing your code to something like this:

foundCon = False
for conTuple in con:
   if uInput.lower() in conTuple:
       foundCon = True
       # perform logic for matching items 
       ...
       duplicate = False
       for c in accepted:
           if c in conTuple:
               duplicate = True
       if duplicate:
           # perform logic for duplicate items
           ... 
if not foundCon:
    # perform logic for no matches
    ...

Upvotes: 0

Alfalfa
Alfalfa

Reputation: 131

Karl Knechtel hit the main point: 'in' won't go inside your nested lists (you have a list where each element is a list). But if you do want to see if an element 'foo' exists in a list of lists 'blah' then:

blah=[['hi','there'],['yo','whatsup','foo']]
if('foo' in [y for y in x for x in blah]):
    print("Yep, it's in there")

Basically, you can quickly flatten 'blah' by using a list comprehension so that it becomes:

blah_flattened = ['hi','there','yo','whatsup']

Upvotes: 0

Karl Knechtel
Karl Knechtel

Reputation: 61519

in is not recursive; it can find ('uk', 'united kingdom') in con, but not 'uk' or 'united kingdom' - neither of those is an element of con.

The simplest (and best) fix (of only this issue) is to check if any of the elements in con contains the input:

if any(uInput.lower() in c for c in con):

Incidentally, it would also be better to

  • just convert uInput to lowercase once and then use it
  • use a set for each of the con elements, and also for accepted - semantically, the set is intended primarily to have things looked up in it (as well as other mathematical set operations), and is optimized for that purpose, at the expense of not maintaining an ordering of the elements
  • store (and check) entire sets of country names within accepted; otherwise, I could type uk once and united kingdom the second time around, and united kingdom would not be in accepted yet and I'd get double credit for that country.

Upvotes: 3

Related Questions