Python Forum
Checking for items in variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking for items in variable
#1
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.
Reply
#2
#!/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!')
     
Reply
#3
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..
Reply
#4
@ToOrdinnaryFolk

Take the solution from buran, it more pythonish.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020