How do i check conditions with a list in python 3

2019-09-04 05:46发布

My name is Edwin i am new to programming but i love to learn. I have a assignment for school and i must build a program that rates passwords. but i have a litle problem now. As you can see i made 3 lists with every character. If i run this program it will not show "uw wachtwoord is Sterk" if the conditions klein and groot and nummers and symbols are true. how do i fix this?

btw i can't make use of isdigit,isnumeric and such.

thank you in advance!

print ("Check of uw wachtwoord veilig genoeg is in dit programma.")
print ("Uw wachtwoord moet tussen minimaal 6 en maximaal 12 karakters
bestaan")

print ("U kunt gebruik maken van hoofdletters,getallen en symbolen 
(@,#,$,%)")
ww = input("Voer uw wachtwoord in: ")


klein = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
groot = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
nummers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
symbolen= [' ', '!', '#', '$', '%', '&', '"', '(', ')', '*', '+', ',', '-',
'.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', 
'{', '|', '}', '~',"'"]


if len(ww) < 6:
    print ("uw wachtwoord is te kort, uw wachtwoord moet uit minimaal 6 en
    maximaal 12 karakters bestaan!")
elif len(ww) > 12:
    print ("uw wachtwoord is te lang, uw wachtwoord moet uit minimaal 6 en  
    maximaal 12 karakters bestaan!")
elif len(ww) >= 6 and len(ww)<= 12:
    if ww == klein and ww == groot and ww == nummers and ww == symbolen:
          print ("uw wachtwoord is Sterk")

1条回答
你好瞎i
2楼-- · 2019-09-04 06:06

Your test cannot work because you're comparing your password (of type str: string) against a list. Since objects are non-comparable, the result is just False (even if they were comparable there is no equality here, but a non-empty intersection to check)

You need to check for each list that there's at least 1 member of the list in the password

Define an aux function which checks if a letter of the list is in the password (a lambda would be too much maybe) using any:

def ok(passwd,l):
    return any(k in passwd for k in l)

Then test all your four lists against this condition using all:

elif len(ww) >= 6 and len(ww)<= 12:
    sww = set(ww)
    if all(ok(sww,l) for l in [klein,groot,nummers,symbolen]):
        print ("uw wachtwoord is Sterk")

Note the slight optimization by converting the password (which is kind of a list so O(n) for in operator) by a set of characters (where the in operator exists but is way faster). Besides, a set will remove duplicate characters, which is perfect for this example.

More compact version without the aux function and using a lambda which is not so difficult to understand after all:

elif len(ww) >= 6 and len(ww)<= 12:
    sww = set(ww)
    if all(lambda l: any(k in sww for k in l) for l in [klein,groot,nummers,symbolen]):
        print ("uw wachtwoord is Sterk")
查看更多
登录 后发表回答