You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Python密码验证脚本添加while循环实现重复输入校验?

密码验证脚本添加循环重试功能解决方案

我已经完成了课堂作业的密码验证脚本,但不知道怎么添加while循环,让密码不符合要求时提示用户重新输入。作业不允许使用正则表达式,我是Python新手,问题应该出在对while循环和布尔值的理解上。虽然作业不要求这个功能,但我完全不知道怎么加,希望得到帮助!

原脚本如下:

def main():
    #Prompting for a new password
    print("Hello! Welcome to newPasswordMaker, below are the requirements for a strong password. Please review the requirements and enter your new password for verification!\n\n")
    print("Your password must meet the following parameters:\n")
    print("- Must be greater than 8 characters and no more than 12 characters.")
    print("- Must not include any spaces.")
    print("- Must contain at least one digit")
    print("- Must contain at least one alphabetic letter\n")

    newPassword = input("Please enter your new password below:\n")
    print(finalCheck(newPassword))  

    
#--------FUNCTIONS---------

#Function to check for valid length of password
def lenChecker(newPassword):
    if len(newPassword) < 8:
        return False
    elif len(newPassword) > 12:
        return False
    return True

#Function to check for presence of spaces
def spaceChecker(newPassword):
    if " " in newPassword:
        return True
    return False    

#Function to confirm newPassword meets requirements of at least alpha and one numeral
def charChecker(newPassword):
    if not any(char.isalpha() for char in newPassword):
        return False
    elif not any(char.isdigit() for char in newPassword):
        return False
    return True   

#Function to pass all values through and return specific feedback!
def finalCheck (newPassword):

    #Final pass to validate length of password:
    if(lenChecker(newPassword)== False):
        return "Your password must be at least 8 but no more than 12 characters."

    #Final pass to check for presence of " " in newPassword:
    elif (spaceChecker(newPassword)== True):
        return "Your password may not have any spaces in it."

    #Final pass for validation of alphabetics and numericals:
    elif (charChecker(newPassword)== False):
        return "Your password should contain at least one alphabetic AND one numerical character"
    #If all checks return as False:
    return "\nYour password passes all checks!\nCongrats on the new password!"


main()

修改方案

核心思路是在main函数里用while循环持续请求用户输入,直到密码通过所有验证。我们可以通过判断finalCheck的返回结果来决定是否继续循环:

def main():
    #Prompting for a new password
    print("Hello! Welcome to newPasswordMaker, below are the requirements for a strong password. Please review the requirements and enter your new password for verification!\n\n")
    print("Your password must meet the following parameters:\n")
    print("- Must be greater than 8 characters and no more than 12 characters.")
    print("- Must not include any spaces.")
    print("- Must contain at least one digit")
    print("- Must contain at least one alphabetic letter\n")

    while True:
        newPassword = input("Please enter your new password below:\n")
        result = finalCheck(newPassword)
        print(result)
        # 判断是否验证成功,成功则退出循环
        if "passes all checks" in result:
            break

#--------FUNCTIONS---------

#Function to check for valid length of password
def lenChecker(newPassword):
    if len(newPassword) < 8:
        return False
    elif len(newPassword) > 12:
        return False
    return True

#Function to check for presence of spaces
def spaceChecker(newPassword):
    if " " in newPassword:
        return True
    return False    

#Function to confirm newPassword meets requirements of at least alpha and one numeral
def charChecker(newPassword):
    if not any(char.isalpha() for char in newPassword):
        return False
    elif not any(char.isdigit() for char in newPassword):
        return False
    return True   

#Function to pass all values through and return specific feedback!
def finalCheck (newPassword):

    #Final pass to validate length of password:
    if(lenChecker(newPassword)== False):
        return "Your password must be at least 8 but no more than 12 characters."

    #Final pass to check for presence of " " in newPassword:
    elif (spaceChecker(newPassword)== True):
        return "Your password may not have any spaces in it."

    #Final pass for validation of alphabetics and numericals:
    elif (charChecker(newPassword)== False):
        return "Your password should contain at least one alphabetic AND one numerical character"
    #If all checks return as False:
    return "\nYour password passes all checks!\nCongrats on the new password!"


main()

关键修改说明

  1. 把原来单次的输入和打印逻辑放到while True循环里,这个循环会一直执行直到遇到break语句。
  2. 每次调用finalCheck后保存结果,打印提示信息。
  3. 检查结果里是否包含验证成功的关键词passes all checks,如果包含就用break退出循环,否则继续让用户输入。

另外修正了原脚本中finalCheck函数的缩进问题(原代码里这个函数前面多了一个空格,会导致语法错误)。

内容的提问来源于stack exchange,提问作者SemperBandito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 19:15:38