【发布时间】:2021-05-07 07:48:16
【问题描述】:
密码必须至少包含八个字符。 • 密码仅由字母和数字组成。 • 密码必须至少包含两位数字
【问题讨论】:
-
这能回答你的问题吗? Validation of a Password - Python
标签: python-3.x
密码必须至少包含八个字符。 • 密码仅由字母和数字组成。 • 密码必须至少包含两位数字
【问题讨论】:
标签: python-3.x
您可以参考此代码作为您的答案:
import re
def validate():
while True:
password = input("Enter a password: ")
if len(password) < 8:
print("Make sure your password is at lest 8 letters")
elif re.search('[0-9]',password) is None:
print("Make sure your password has a number in it")
elif re.search('[A-Z]',password) is None:
print("Make sure your password has a capital letter in it")
else:
print("Your password seems fine")
break
validate()
【讨论】:
如果您想生成此类密码,请使用以下代码:
import string
import random
letters =string.ascii_letters
digits = string.digits
comb = letters + digits
length = random.randint(6,98)
password = random.choices(digits,k = 2)
password+= random.choices(comb, k =length)
random.shuffle(password)
password = ''.join(password)
print(password)
我假设密码的最大长度为 100。您可能需要更改它。
【讨论】: