[英]How to force user to re-enter the input if it doesn't meet requirement with for loop?
这是我的代码,要求用户输入他们想要生成的密码字符数。 如果他们输入数字 < 8,我想强制他们重新输入。 否则,将生成密码。 但我被 for 循环卡住了,因为它产生了无限期的打印。
任何帮助,将不胜感激。
import string
import random
lower_string = string.ascii_lowercase
upper_string = string.ascii_uppercase
special_string = "!@#$%&*()[]{}"
number = '0123456789'
list_string = [lower_string, upper_string, special_string, number]
password = ''
password_length = int(input("Enter length of password: "))
while password_length < 8:
print("Hey, this password's length is not good. Enter > 8")
continue
else:
for _ in range(password_length):
x = random.choice(random.choice(list_string))
password = password + x
print(password)
尝试将input
语句放在 while 循环中
while True:
password_length = int(input("Enter length of password: "))
if password_length < 8:
print("Hey, this password's length is not good. Enter > 8")
else:
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.