Search code examples
pythonuser-input

Are there any statements that can be used to run the code(s) again?


import random
import sys


print("Password Generator")    
print("* Attention: - You may not generate a password with more than 70 characters\n"
      "             - You may not enter letters")

characters = "abcdefghijklmnopqrstuvwxyz" \
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"
pass_length = input("Enter your password length: ")

Here I just want to make sure the information that the user's input are numbers, not letters!

if pass_length.isnumeric() is False:
    sys.exit("Natural numbers only!")

Is there a statement that can automatically run the script again after the "if" statement above is activated?

 def password_gen_start():
        password = "".join(random.sample(characters, int(pass_length)))
        print("Your password has been generated: " + format(password))


if int(pass_length) >= 71 and pass_length.isnumeric() is True:
    sys.exit("Password cannot be longer than 70 characters!")

if int(pass_length) < 71 and pass_length.isnumeric() is True:
    password_gen_start()

Solution

  • You just have to put it in a while loop :

    stop = ""
    while stop != "yes":
        pass_length = input("Enter your password length: ")
        while not pass_length.isnumeric():
            pass_length = input("Natural numbers only !\n Enter your password length: ")
        # you put here the stuff that you want to execute
        stop=input("do you want to stop ?")