【发布时间】:2022-01-14 06:56:58
【问题描述】:
我是 Python 新手(2 周),课外一直在研究随机密码生成器。 我已经制作了密码生成器;但是,我不确定如何开始为用户生成另一个密码的过程。到目前为止,我已经尝试过创建一个循环,但很快就迷路了。关于我做错了什么,我最好的猜测是我只是不太了解循环,而创建循环将解决我的问题。
# This greets the user
print('Hello, Welcome to this random password generator')
# This asks the user for the length of the password
length = int(input('\nEnter the length of the password you would like to generate: '))
# This defines data for the string module
lower = string.ascii_lowercase
upper = string.ascii_uppercase
number = string.digits
# This combines the data
all = lower + upper + number
# This allows us to randomize the data collected
temp = random.sample(all, length)
# This generates the password
password = "".join(temp)
# This will generate a password of up to 94 characters
print(password)
# This prompts the user to generate another password
next_generation = input("Would you like to generate another password? (yes/no): ")
【问题讨论】:
-
请不要将代码作为图片发布,将代码复制并粘贴到帖子本身中。
-
刚刚知道怎么做,谢谢你的提示!
-
目前还不清楚您想在代码中的何处或如何使用循环。但是,使用预制模块可以轻松生成密码。 this 有帮助吗?
-
@Lucas 感谢您的帮助!我不认为你可以做得更简单。我想我还有很多东西要学。至于循环,我不确定它是否是最好的做法。我只是想在打印初始密码后为用户生成另一个密码。