pythonliststring-comparisonwhitelist

Doesn't compare lists correctly


def split(word):
    x = list(word)
    return x

whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']

word = input('Password ')
split(word)
x = split(word)

if x not in whitelist:
    print('Incorrect letters')
else: 
    print('Correct ')

When I enter FECHCH for example, 'if' keeps giving me communicate - incorrect letters.

I need Python3 to understand, that letters FECHCH are on the whitelist.


Solution

  • You need to enter letters one by one

    def split(word):
        x = list(word)
        return x
    
    whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']
    
    word = input('Password ')
    split(word)
    x = split(word)
    print(x)
    
    #Password FECHCH
    ['F', 'E', 'C', 'H', 'C', 'H']
    

    so you are comparing:

    ['F', 'E', 'C', 'H', 'C', 'H'] in ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']
    

    which is False

    as mentioned in comments, you can use a set() to remove duplicates and check subsets.

    word = input('Password ')
    split(word)
    x = split(word)
    
    if set(x).issubset(whitelist):
        print('Correct ')
        
    else: 
        print('Incorrect letters')