-1

I am fairly new to python, and I have a random password generator where the user types out the word 'generate' (my take on a button, if there is a better way please tell me) to get a new word. It generates a random word, joins it with a random number, then again a random word and then prints it. The issue is that I have just copied and pasted the code that runs each time, to be able to keep generating new passwords. What command can I use to loop this question?. Heres my code:

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))
  • 2
    Does this answer your question? [How do I restart my program in Python? (see code)](https://stackoverflow.com/questions/44851836/how-do-i-restart-my-program-in-python-see-code) – Corentin Pane May 24 '20 at 00:57

1 Answers1

0

You can use a while loop to repeat until a certain condition is met.

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")

The program will keep repeating until the user enters quit, which will then break the while loop.

Brenden Price
  • 517
  • 2
  • 9