程序循环如果第二次遇到强密码

时间:2014-10-18 18:57:00

标签: python

我有一个奇怪的问题,我不知道为什么会这样。我觉得很难解释,但在这里。我的程序工作正常,检查长度甚至力量。我的问题是如果输入以任何方式第一次失败,然后重新输入并且是正确的长度和强度它将重新请求密码输入。我认为这是因为最后一个像passwordbegin(),但如果我删除它,程序显然不会运行。任何人都可以给我一个提示或向我解释为什么会这样吗?提前谢谢!

def passwordbegin():      
        Upper = 0 
        Lower = 0  
        Number = 0 
        pass_check = True 
        while pass_check is True:  
            passw = raw_input("Please enter your password. It must be between 6 and 12 characters long and contain atleast capital letter, lowercase letter and number.")

            if len(passw) < 6: 
                print("Your password is too short, it must be at least 6 characters long")  
            elif len(passw) > 12:
                print("Your password is too long, it must be no more than 12 characters long")  
            else:
                print("Your password is the correct length.")   
                pass_check = False  

            if pass_check is False:  
                for char in passw:  
                    if char.isupper():  
                        Upper =+ 1  
                    if char.islower():
                        Lower =+ 1  
                    if char.isdigit():
                        Number =+ 1   
            Password_Strength = Number + Lower + Upper          
            if Password_Strength == 1:
                print("Your password is weak, trying adding Numbers or Upper and Lowercase Letters") 
                raw_input("Press enter to restart the program")  
                while True:
                    passwordbegin() 
            if Password_Strength == 2:  
                print("Your password is medium strength, try adding Upper or Lowercase Letters and Numbers") 
                raw_input("Press enter to restart the program")
                while True:
                    passwordbegin()
            elif Password_Strength == 3:
                print("Your password is strong and secure, thank you for entering!")
    passwordbegin()

1 个答案:

答案 0 :(得分:1)

不要使用while pass_check is True:它不会按照您的想法执行。

你应该使用while pass_check == True甚至更好的`pass_check:

主要问题是你再次调用你的函数,而不是让它循环。

而不是调用passwordbegin() - 使用continue(回到循环的开头),你不需要在while True:循环中继续。