具有字数限制的随机密码生成器
Random password generator with words limit
我正在创建一个随机密码生成器。我想创建一个输入字段,其中密码长度只能是最小 8 和最大 16。如果用户输入的长度不是允许的长度,它会不断要求用户输入正确的密码长度。我使用 'return' 所以它 return 到 length = int(input('\nEnter the length of password: '))
,但它在这里不起作用。我可以获得有关如何正确执行此操作的建议吗?
#input the length of password
#must be minimun 8 to maximun 16
length = int(input('\nEnter the length of password: '))
#if use enter less than 8 or more than 16
if length < 8 :
print('You password length is too short(must be more than 8 character)')
return;
elif length > 16:
print('You password length is too long(must be less than 17 character)')
return;
else:
print('You password length looks good')
改用内置 len
函数并使用 while 循环:
#input the length of password
#must be minimun 8 to maximun 16
#if use enter less than 8 or more than 16
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
输出:
Enter the length of password: thisismorethan16characters
You password length is too long(must be less than 17 character)
26 is the length of your password
Enter the length of password: thisisgood
You password length looks good
>>>
此外,删除 int()
以便我们可以允许用户输入字符串,而您通常在函数中使用 return
。 python.
中也没有使用 ;
the code will run till you enter a correct password. I declared a variable correct_pass set to false. with condition correct_pass == False in while loop when the user enters correct password the value of correct_pass is changed to True and hence it exits out of the loop and in other cases it value remains unchanged
correct_pass=False
while(correct_pass==False):
length = input('\nEnter the length of password: ')
if len(length) < 8 :
print('You password length is too short(must be more than 8 character)')
correct_pass=False
elif len(length) < 8:
print('You password length is too long(must be less than 17 character)')
correct_pass=False
else:
print('You password length looks good')
correct_pass=True
您的代码基本上使用的是整数值,而不是用户输入的长度或整数个数。
假设我输入 9。
那么,您正在使用 If-elif-else 块。
如果长度(此时用户输入为 9)<8,打印“您的密码长度太短”。在我们的例子中,长度等于9,也小于17。所以我们满足条件。
但是如果我们输入,例如 0,2222。这显然比您的代码要求的值大得多。我希望我把你的错误弄清楚了。现在,修复方法是:
我正在创建一个随机密码生成器。我想创建一个输入字段,其中密码长度只能是最小 8 和最大 16。如果用户输入的长度不是允许的长度,它会不断要求用户输入正确的密码长度。我使用 'return' 所以它 return 到 length = int(input('\nEnter the length of password: '))
,但它在这里不起作用。我可以获得有关如何正确执行此操作的建议吗?
#input the length of password
#must be minimun 8 to maximun 16
length = int(input('\nEnter the length of password: '))
#if use enter less than 8 or more than 16
if length < 8 :
print('You password length is too short(must be more than 8 character)')
return;
elif length > 16:
print('You password length is too long(must be less than 17 character)')
return;
else:
print('You password length looks good')
改用内置 len
函数并使用 while 循环:
#input the length of password
#must be minimun 8 to maximun 16
#if use enter less than 8 or more than 16
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
输出:
Enter the length of password: thisismorethan16characters
You password length is too long(must be less than 17 character)
26 is the length of your password
Enter the length of password: thisisgood
You password length looks good
>>>
此外,删除 int()
以便我们可以允许用户输入字符串,而您通常在函数中使用 return
。 python.
;
the code will run till you enter a correct password. I declared a variable correct_pass set to false. with condition correct_pass == False in while loop when the user enters correct password the value of correct_pass is changed to True and hence it exits out of the loop and in other cases it value remains unchanged
correct_pass=False
while(correct_pass==False):
length = input('\nEnter the length of password: ')
if len(length) < 8 :
print('You password length is too short(must be more than 8 character)')
correct_pass=False
elif len(length) < 8:
print('You password length is too long(must be less than 17 character)')
correct_pass=False
else:
print('You password length looks good')
correct_pass=True
您的代码基本上使用的是整数值,而不是用户输入的长度或整数个数。
假设我输入 9。
那么,您正在使用 If-elif-else 块。
如果长度(此时用户输入为 9)<8,打印“您的密码长度太短”。在我们的例子中,长度等于9,也小于17。所以我们满足条件。
但是如果我们输入,例如 0,2222。这显然比您的代码要求的值大得多。我希望我把你的错误弄清楚了。现在,修复方法是: