坚持我已经制作的代码

时间:2015-10-11 17:52:39

标签: python python-2.7 passwords

此代码应该说"请输入新密码"如果您从我创建的列表中输入一个公共密码,但它没有,它只是说"密码接受"不应该这样。有人可以帮我吗?这是代码本身:

### 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

2 个答案:

答案 0 :(得分:1)

你想做的可能是:

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"

答案 1 :(得分:0)

尝试if password in common_passwords,它将一次性测试所有常用密码。 你当前的错误是当你遍历每一个时,如果第一个中的元素不匹配,那么它将接受它。