[英]Random Password Generator Keeps Generating the Same Password
I have to write a program that generates random passwords (using ASCII values and the chr() function) using Python and I have gotten my program to generate a random password, but when the program loops, it keeps printing the same random password and I have no idea what to do to fix it. 我必须编写一个使用Python生成随机密码(使用ASCII值和chr()函数)的程序,并且已经获得了生成随机密码的程序,但是当程序循环时,它会继续打印相同的随机密码,不知道该如何解决。 If anyone could give me some advice, that would be great because I'm new to python 如果有人可以给我一些建议,那将很棒,因为我是python新手
here is the code that I have so far: 这是我到目前为止的代码:
import random, string
LNGTH=8
position=0
password=""
start=0
stop=0
while start==stop:
input("would you like a password?")
while position<LNGTH:
x=random.randrange(9)
character=chr(random.randrange(97, 97 + 26))
choice=[str(x),character.upper(),character.lower()]
pass_pos=random.choice(choice)
password=password+pass_pos
position+=1
print(password)
You never reset position
, so your while
loop always returns True
after the first password is generated. 您永远不会重置position
,因此您的while
循环在生成第一个密码后始终返回True
。
Because you also do not reset password
that means you generate just the one random password and re-display that value over and over again. 因为您也不重置password
,这意味着您仅生成一个随机密码,然后一次又一次地重新显示该值。
You need to reset both inside the outer while
loop: 您需要到外内复位while
循环:
while start==stop:
input("would you like a password?")
position = 0
password = ''
You could just use while True
instead of start == stop
there. 您可以只使用while True
而不是start == stop
在那里start == stop
。 You could also use a for
loop instead of your inner while
loop, simplifying your setup: 您还可以使用for
循环而不是内部while
循环,从而简化设置:
password = ''
for i in range(LNGTH):
x=random.randrange(9)
character=chr(random.randrange(97, 97 + 26))
choice=[str(x),character.upper(),character.lower()]
pass_pos=random.choice(choice)
password=password+pass_pos
Cleaning that up some more by using string.ascii_letters
and string.digits
: 通过使用string.ascii_letters
和string.digits
清理更多string.digits
:
import string
import random
password_characters = string.ascii_letters + string.digits
length = 8
while True:
if input("Would you like a password? ").lower() not in {'y', 'yes'}:
break
password = ''.join([random.choice(password_characters)
for _ in range(length)])
print(password)
Once you have been through the loop once, position
is equal to LNGTH
, so the loop while position<LNGTH:
will exit immediately. 循环一次后, position
等于LNGTH
,所以while position<LNGTH:
时的循环将立即退出。
Set position
back to zero and password
back to ""
inside the outer loop 在外循环中将position
设置为零, password
设置为""
Your code only generates one password and keeps printing it. 您的代码仅生成一个密码并继续打印。 That's because it runs the loop while position < LNGTH
just once: inside the loop, you increase the position until the password is long enough. 这是因为它while position < LNGTH
仅运行一次循环:在循环内,您增加位置直到密码足够长。 But during next iterations, position
will be already long enough - so the while
loop won't be entered; 但是在下一次迭代期间, position
将已经足够长-因此将不会输入while
循环; instead, the already previously generated password will be printed. 相反,将打印先前生成的密码。
You have to reset position
to zero before entering the while
loop. 在进入while
循环之前,您必须将position
重置为零。
At that point your program did everytime the same, so without initialising random you get everytime the same random. 那时,您的程序每次都执行相同的操作,因此无需初始化随机数,您就可以每次获得相同的随机性。 you should use random.seed() first or even better use random.SystemRandom() if available. 您应该首先使用random.seed(),甚至最好使用random.SystemRandom()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.