所以在这里我再次改进了我的在线登录功能。
无论如何,我已经完成了大部分工作,但我似乎无法让密码验证完全正常工作。如果密码少于7个字符,它会告诉人们密码无效,但如果密码中没有大写字母,我希望密码也一样。
我花了最后20分钟浏览网页并尝试了很多不同的东西,似乎没有任何工作,但最小的数字却有。无论如何,我的代码atm:
password = input("Enter a password: ")
capital = password.upper().isupper()
while len(password) < 7 and capital is False:
print("Your password must be at least 7 characters long including A capital letter")
password = input("Enter a password: ")
答案 0 :(得分:2)
要检查密码是否包含至少一个大写字母,您可以使用:
has_uppercase = any(c.isupper() for c in password)
请参阅any功能的文档。
例如:
>>> any(c.isupper() for c in "secr3t")
False
>>> any(c.isupper() for c in "Secr3t")
True
由于Python没有do ... while ...
循环,你可以像这样使用无限循环:
while True:
password = input("Enter a password: ")
if len(password) > 7 and any(c.isupper() for c in password):
break
print("Your password must be at least 7 characters long including A capital letter")
print("What a secured password!")
您可以尝试一些密码:
Enter a password: secret23
Your password must be at least 7 characters long including A capital letter
Enter a password: Secr3t
Your password must be at least 7 characters long including A capital letter
Enter a password: Secr3t123
What a secured password!