【发布时间】:2026-01-27 20:15:02
【问题描述】:
我正在尝试制作一个程序,当用户第一次创建帐户并且必须输入新密码时执行密码验证。
密码必须超过8个字母,包含一个大写字母、一个小写字母和一个数字。
我已经完成了以下代码,但目前无法正常工作。我认为这是因为islower() 仅在字符串中的所有字符都较低时才有效,与 isupper 相同,但对于大写字母。我该如何解决这个问题,以获得满足要求的干净整洁的解决方案?谢谢
while loop:
pass1 = input('type a password: ')
if len(pass1) > 8 and pass1.islower() and pass1.isupper() and pass1.isdigit():
print('password successful')
break
else:
print('Password not valid, please retry')
【问题讨论】:
-
.islower()检查 all 字符是否为大写。当然,.isupper()和.islower()不能同时为真。 -
好的,感谢您的确认,这就是我认为的问题所在,但仍然不知道如何解决这个问题。
-
创建一个类似
for c in pass1:的循环。这将遍历整个 pw,而c是每次迭代中的当前字符。在那里,您可以使用isupper/islower/isdigit并将您观察到的内容存储在变量中。
标签: python while-loop passwords