坚持我制作的这段代码
Stuck with this code I have made
如果您从我创建的列表中输入一个通用密码,此代码应该显示 "please enter a new password",但它不会,它只是显示 "Password Accepted",但不应该。谁能帮帮我吗?这是代码本身:
### Asks user for their name and password ###
print "Hello, what is your name?"
### Stores name in variable myName ###
myName = raw_input()
print "Nice to meet you "+myName+", please enter a password to continue."
common_passwords = [ '123456', 'password', '12345', '12345678', 'qwerty',
'1234567890', 'baseball', 'dragon', 'football', '1234567',
'monkey', 'letmein', 'abc123', '111111', 'mustang', 'access',
'shadow', 'master', 'michael', 'superman', '696969', '123123',
'batman', 'trustno1']
while True:
password = raw_input()
if len(password) < 8:
print "Password is less than 8 characters."
print "Your password has to be 8 characters or more."
continue
found = False
for cpass in common_passwords:
if cpass == password:
print "You have selected a common password"
print "Please choose a new password"
found = True
break
if not found:
print "Password Accepted"
break
你想做的可能是:
for cpass in common_passwords:
if cpass == password:
print "You have selected a common password"
print "Please choose a new password"
found = True
break
if not found:
print "Password Accepted"
因为你要检查输入的密码是否在common_password
中,如果不是在你的循环之后,密码就可以了。
此外,您可以简单地检查密码是否存在:
If password in common_passwords:
print "You have selected a common password"
print "Please choose a new password"
else :
print "Password Accepted"
尝试if password in common_passwords
,它将一次测试所有常用密码。
您当前的错误是,当您遍历每个元素时,如果 commom_passwords 中的元素如果第一个不匹配,它将接受它。
如果您从我创建的列表中输入一个通用密码,此代码应该显示 "please enter a new password",但它不会,它只是显示 "Password Accepted",但不应该。谁能帮帮我吗?这是代码本身:
### Asks user for their name and password ###
print "Hello, what is your name?"
### Stores name in variable myName ###
myName = raw_input()
print "Nice to meet you "+myName+", please enter a password to continue."
common_passwords = [ '123456', 'password', '12345', '12345678', 'qwerty',
'1234567890', 'baseball', 'dragon', 'football', '1234567',
'monkey', 'letmein', 'abc123', '111111', 'mustang', 'access',
'shadow', 'master', 'michael', 'superman', '696969', '123123',
'batman', 'trustno1']
while True:
password = raw_input()
if len(password) < 8:
print "Password is less than 8 characters."
print "Your password has to be 8 characters or more."
continue
found = False
for cpass in common_passwords:
if cpass == password:
print "You have selected a common password"
print "Please choose a new password"
found = True
break
if not found:
print "Password Accepted"
break
你想做的可能是:
for cpass in common_passwords:
if cpass == password:
print "You have selected a common password"
print "Please choose a new password"
found = True
break
if not found:
print "Password Accepted"
因为你要检查输入的密码是否在common_password
中,如果不是在你的循环之后,密码就可以了。
此外,您可以简单地检查密码是否存在:
If password in common_passwords:
print "You have selected a common password"
print "Please choose a new password"
else :
print "Password Accepted"
尝试if password in common_passwords
,它将一次测试所有常用密码。
您当前的错误是,当您遍历每个元素时,如果 commom_passwords 中的元素如果第一个不匹配,它将接受它。