当密码不符合if语句不返回raw_input时

时间:2014-02-28 08:18:32

标签: python passwords raw-input

功能检查用户的密码是否符合,只要字母数字字符,最小长度为10个字符,以及小写和大写字符。

def is_good_password(password):
    count_upper, count_lower = 0, 0
    for characters in password:
        if characters.isupper():
            count_upper += 1
        if characters.islower():
            count_lower += 1
    is_password_good = True
    if len(password) <= 10:
        print "Password Is Too Weak, Must Be More Than 10 Characters Long!"
        is_password_good = False
    if set(database).intersection(password):
        print "Password Must Contain Alphanumeric Characters!"
        is_password_good = False
    if count_upper < 1 or count_lower < 1:
        print "Password Must Contain at Least One Uppercase and One Lowercase Character!"
        is_password_good = False
        create_user(database)
    print "Welcome! Username & Password Successfully Created!"
    return is_password_good

如果用户的密码不符合上述功能,我希望create_user()函数raw_input for passcode返回密码;但是,如果密码不起作用,create_user()函数会将密码raw_input返回给用户raw_input。

我该如何解决这个问题? 感谢

def create_user(database):
    good_user = False
    good_pass = False
    while not good_user or not good_pass:
        user = raw_input("Enter a New Username: ")
        good_user = is_good_user(user)
        passcode = raw_input("Enter a New Password: ")
        good_pass = is_good_password(passcode)
        database[user] = passcode
        dump_data()

2 个答案:

答案 0 :(得分:2)

添加另一个循环;您也不需要使用标志变量,只需使用break在有良好的用户或密码时结束循环:

while True:
    user = raw_input("Enter a New Username: ")
    if is_good_user(user):
        break
    print "That's not a good username, please try again"

while True:
    passcode = raw_input("Enter a New Password: ")
    if is_good_password(passcode):
        break
    print "That's not a good password, please try again"

database[user] = passcode
dump_data()

答案 1 :(得分:0)

我理解正确吗?

def create_user(database):
  good_user = False
  good_pass = False
  while not good_user:
    user = raw_input("Enter a New Username: ")
    good_user = is_good_user(user)
  while not good_pass:
    passcode = raw_input("Enter a New Password: ")
    good_pass =  is_good_password(passcode)
  database[user] = passcode
  dump_data()