我正在创建一个随机密码生成器。第一个我必须询问用户密码的长度,它必须至少有 8 位,最多 16 位。我创建的一个是要求用户输入密码本身,然后从那里检查长度。首先,我希望用户输入密码的长度,例如:7 或 9 等。如果用户键入小于 8 且大于 16 的数字,则必须显示“必须至少有 8 位到最多 16 位”。请参考下面的代码,如果不清楚,请参考这两个图像。谢谢你。
输入
import random
import string
print('hello, Welcome to Password generator!')
l = False
while not l:
length = input('\nEnter the length of password: ')
if len(length) < 8 :
print('You password length is too short(must be more than 8 character)')
print(len(length), "is the length of your password")
elif len(length) > 16:
print('You password length is too long(must be less than 17 character)')
print(len(length), "is the length of your password")
else:
print('You password length looks good')
break
lower = string.ascii_lowercase
upper = string.ascii_uppercase
num = string.digits
symbols = string.punctuation
all = lower + upper + num + symbols
temp = random.sample(all,length)
password = "".join(temp)
print(password)
输出
hello, Welcome to Password generator!
Enter the length of password: 9
You password length is too short(must be more than 8 character)
1 is the length of your password
Enter the length of password: 9
You password length is too short(must be more than 8 character)
1 is the length of your password