Python Forum

Full Version: Checking for items in variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a very simple issue that i just can't get right.
It is strange because i remember being able to do this. But now the code i always used to use no longer works (although i never updated my python version 3.4.3)

while True:
password = input('Enter a password:')
illegalSymbols = ['!','@','#','$','%','^','&','*','(',')']
for i in illegalSymbols[range(1,10)]:
if i in password == True:
print('Your password contains not allowed symbols!')
#This simple loop is supposed to identify symbols that are not allowed and give an error message.

#I tried every possible combination, loop anything that i could think of but this just isn't happening.
#!/usr/bin/python3

while True:
    password = input('Enter a password:')
    illegalSymbols = ['!','@','#','$','%','^','&','*','(',')']
    for char in illegalSymbols:
        if char in password:
            print('Your password contains not allowed symbols!')
     
it is better to use
if any(char in illegalSymbols for char in password):
    print('Your password contains not allowed symbols!')
this way it will print the warning just once, despite how many illegal symbols there are..
@ToOrdinnaryFolk

Take the solution from buran, it more pythonish.