gpt4 book ai didi

python - 如何在 python 中检查 "password"中的数字

转载 作者:行者123 更新时间:2023-11-28 22:30:51 25 4
gpt4 key购买 nike

我已经设置了一个程序来更改“密码”。我让它检查它是否至少有 8 个字符,包含一个大写字母和一个数字,如果不符合这个标准,它会再次要求输入密码。除了检查号码外,我一切正常,我想知道是否有人可以提供帮助。

npwv = 1

while npwv == 1:

npw = input("Please enter new password.")
npwc = input ("Please confirm new password")

if npwc == npw:
if npwc.isupper()== False:
if npwc.islower()== False:
if len(npwc) >= 8:
if str.isdigit(npwc) == True:
npw=npwc
print("Your password has been changed")
else:
print("Your password must contain a number")
npwv = 1
else:
print("Your password must contain at least 8 characters.")
npwv = 1
else:
print("Your password must contain at least 1 upper case character.")
npwv = 1

else:
print ("Passwords don't match")
npwv = 1

最佳答案

您正在检查密码本身是完全大写还是由数字组成。您需要检查密码中的字符是否符合此条件。

has_upper = any([c.isupper() for c in npwc])
has_digit = any([c.isdigit() for c in npwc])

您还可以使用正则表达式。

顺便说一句,你应该更喜欢getpass从用户那里获取密码。

关于python - 如何在 python 中检查 "password"中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41941421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com