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

如何修复Python密码验证程序误报缺少小写字符的问题

问题修复方案

核心错误原因

你遇到的密码误报问题,根源是str.islower()、str.isupper()两个方法的用法理解错误:

  • str.islower()的判定规则是字符串中所有字母全为小写,且至少有一个字母才返回True,你的测试密码Pass1234包含大写字母P,所以p.islower()返回False,触发了not p.islower()的错误提示,属于逻辑判定错误。
  • 同理str.isupper()是所有字母全为大写才返回True,也不符合“至少包含一个大写/小写”的要求。

除此之外你的代码还有多个不符合需求的问题:

  • 密码长度要求是至少7位,原代码写的是6位
  • 所有输入项都没有做循环校验,输入错误仅提示一次就继续执行,不符合“无效则循环重输”的要求
  • 密码掩码逻辑写死了固定替换后5位,无法适配不同长度的密码
  • 名字验证逻辑错误:if fn == first_name or 'john'这种写法永远为真,且没有验证非数字的其他特殊字符
  • ID验证逻辑错误:用isupper/islower判定是否为数字完全不对,应该用str.isdigit()
  • 大量逻辑判断存在漏洞,没有按需求完成校验规则

修复后完整代码

# 预设正确值
correct_pwd = "Pass1234"
correct_firstname = "John"
correct_lastname = "Smith"
correct_id = "1234"

# 1. 密码校验(循环直到输入正确)
while True:
    p = input("Input your password: ")
    # 校验长度
    if len(p) < 7:
        print("Invalid password. Must be at least 7 characters")
        continue
    # 校验是否包含至少1个大写、小写、数字
    has_upper = any(c.isupper() for c in p)
    has_lower = any(c.islower() for c in p)
    has_digit = any(c.isdigit() for c in p)
    if not has_upper:
        print("Invalid password. Must have at least 1 uppercase character")
        continue
    if not has_lower:
        print("Invalid password. Must have at least 1 lowercase character")
        continue
    if not has_digit:
        print("Invalid password. Must have at least 1 digit")
        continue
    # 校验密码是否匹配预设
    if p != correct_pwd:
        print("Wrong password, please try again")
        continue
    print("Well done! This is a valid password")
    break

# 密码掩码:取前3位,后面全用*代替
masked_pwd = p[:3] + '*' * (len(p) - 3)

# 2. 名字校验
while True:
    fn = input("Please enter your first name: ")
    if not fn.isalpha():
        print("Invalid Name! Name should only contain letters")
        continue
    if fn != correct_firstname:
        print("Wrong first name, please try again")
        continue
    break
while True:
    ln = input("Please enter your last name: ")
    if not ln.isalpha():
        print("Invalid Name! Name should only contain letters")
        continue
    if ln != correct_lastname:
        print("Wrong last name, please try again")
        continue
    break
print("Well done! This is a valid name")

# 3. ID校验
while True:
    user_id = input("Please enter your ID: ")
    if not user_id.isdigit():
        print("Invalid ID! ID should only contain numbers")
        continue
    if user_id != correct_id:
        print("Wrong ID, please try again")
        continue
    print("Well done! This is a valid ID")
    break

# 输出最终信息
print("Name:", correct_firstname, correct_lastname)
print("ID:", correct_id)
print("Password:", masked_pwd)

关键修复点说明

  • 密码校验改用any()遍历每个字符判定是否存在大写/小写/数字,符合需求中“至少包含一个”的要求
  • 所有输入项都用while True实现循环校验,输入无效就一直要求重输,直到符合规则且匹配预设值
  • 密码掩码用切片+字符串乘法实现,适配任意长度的密码
  • 名字校验直接用str.isalpha()判定是否全为字母,覆盖所有非字母的非法字符
  • ID校验用str.isdigit()直接判定是否全为数字,逻辑准确
  • 修正了原代码中所有逻辑运算符使用错误的问题,避免永远为真的无效判断

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 18:36:07