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

含len()的if语句未执行:Python密码验证程序异常排查

编辑:此问题由Python/IDE安装故障导致,重装IDE后代码运行正常。

我编写了一个简单的密码设置程序,要求用户两次输入密码,若两次输入一致则提示“Password set.”。但问题在于,程序跳过了检查密码长度需在8-12字符之间的第一个if语句,希望了解该问题的原因。

程序代码

pswrd = input('Please choose a password: ')
if len(pswrd) < 8 or len(pswrd) > 12:
    print('The password you have chosen is invalid. Please create a password between 8-12 characters.')
else:
    pswrd2 = input('Please retype your new password for confirmation: ')
if pswrd2 == pswrd:
        print('Thank you. Your password has been set.')
else:
    print('Your two entries do not match. Please try again.')

程序输出

Please choose a password: seven
Please retype your new password for confirmation: seven
Thank you. Your password has been set.

Process finished with exit code 0

如您所见,程序未识别到密码长度不足8字符,这是为什么?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 14:40:32