如何验证输入以检查其是否包含至少1个数字,一个大写字母和一个小写字母。我查看了类似的问题,但它们似乎并不是我所需要的,例如只检查数字和拒绝信件,我需要它,除了任何有一个以上的数字,大写字母和小写字母的内容。
答案 0 :(得分:0)
您可以将assert
与any()
和string
内置模块结合使用,如下所示:
import string
password = '1Aa'
try:
assert any(i in string.ascii_lowercase for i in password)
assert any(i in string.ascii_uppercase for i in password)
assert any(i in string.digits for i in password)
except AssertionError as e:
raise Exception('Invalid password!')
答案 1 :(得分:0)
import re
def passwordagain():
password()
def password():
print()
password = input("Enter a secure password: ")
if re.search('[A-Z]',password) is None:
print("Password must contain at least one capital letter and one number.")
passwordagain()
elif re.search('[0-9]',password) is None:
print("Password must contain at least one capital letter and one number.")
passwordagain()
elif re.search('[a-z]',password) is None:
print("Password must contain at least one capital letter and one number.")
passwordagain()
else:
filename = ("password.");
with open (filename, "w") as f:
f.write (password)
print()
print("Password saved")