python - 密码检查程序,而循环不工作

时间:2017-12-02 19:32:02

标签: python python-3.x

我正在制作一个密码验证器/检查器程序作为我的计算任务的一部分。它必须有一个大写和小写字母,并且长度至少为8个字符。 到目前为止,我已经这样做了:

new_password = input("Please enter your new password: ")
new_password2 = input("Please enter your new password again: ")

while new_password != new_password2:
    print("The passwords don't match up.")
    new_password = input("Please enter your new password: ")
    new_password2 = input("Please enter your new password again: ")

if new_password == new_password2:
    length = len(new_password)
    while int(length) < 8:
        print("Your password must be longer")
        new_password = input("Please enter your new password: ")
        new_password2 = input("Please enter your new password again: ")      
    letters = set(new_password)
    lower = any(letter.islower() for letter in letters)
    while new_password == new_password2:
        if not lower:
            print("Your password must contain a lowercase letter")
            new_password = input("Please enter your new password: ")
            new_password2 = input("Please enter your new password again: ")
    upper = any(letter.isupper() for letter in letters)
    while new_password == new_password2 :
        if not upper:
            print("Your password must contain an uppercase letter")
            new_password = input("Please enter your new password: ")
            new_password2 = input("Please enter your new password again: ")

代码运行,但由于某种原因,即使条件正确,while循环也不起作用(例如,密码包含大写字母),用户再次输入密码的选项正在显示。有人可以看看,告诉我这里的问题吗?感谢

2 个答案:

答案 0 :(得分:0)

您希望只有一个while循环,其中检查每个需求。请参阅以下代码:

valid_password = False

while not valid_password: 
    new_password = input("Please enter your new password: ")
    new_password2 = input("Please enter your new password again: ")

    if new_password != new_password2:
        print("The passwords don't match up.")
        continue
    elif len(new_password) < 8:
        print("Your password must be longer")
        continue
    elif new_password.upper() == new_password or new_password.lower() == new_password:
        print("Your password must contain at least one lowercase and uppercase letter")
        continue
    else:
        print("Password Accepted!")
        valid_password = True

希望这有帮助!

答案 1 :(得分:0)

a= int (input("Enter Passcode: "))
if a == 1974:
   print ("     Welcome!!    ")

else:
   print ("Wrong Passcode")
   print ("Run again")