我对python相当陌生,我有一个随机密码生成器,用户在其中输入“生成”这个词(我对一个按钮的看法,如果有更好的方法请告诉我)来获得一个新词。它生成一个随机单词,将它与一个随机数连接起来,然后再次生成一个随机单词,然后将其打印出来。问题是我刚刚复制并粘贴了每次运行的代码,以便能够不断生成新密码。我可以使用什么命令来循环这个问题?。这是我的代码:
value = input("Type 'generate' to generate a new password.\n")
if value == 'generate':
line = open("words.txt").read()
line = line[0:]
words = line.split()
one = random.choice(words)
line = open("numbers.txt").read()
line = line[0:]
numbers = line.split()
two = random.choice(numbers)
line = open("words.txt").read()
line = line[0:]
words2 = line.split()
three = random.choice(words2)
print('Your new password is '+(one)+(two)+(three))
您可以使用 while 循环重复直到满足特定条件。
value = input("Type 'generate' to generate a new password or 'quit' to quit.\n")
while value != 'quit':
if value == 'generate':
line = open("words.txt").read()
line = line[0:]
words = line.split()
one = random.choice(words)
line = open("numbers.txt").read()
line = line[0:]
numbers = line.split()
two = random.choice(numbers)
line = open("words.txt").read()
line = line[0:]
words2 = line.split()
three = random.choice(words2)
print('Your new password is '+(one)+(two)+(three))
else:
print('Invalid input')
value = input("Type 'generate' to generate a new password or 'quit' to quit.\n")
程序将不断重复,直到用户输入退出,然后将中断 while 循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句