1
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()
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45

2 Answers2

0

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 ?")
Nicolas
  • 51
  • 12
0

Thank you Nicolas, now I can finally finish my code:

import random

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: ")
while not pass_length.isnumeric() or int(pass_length) >= 71:
    pass_length = input("Natural numbers no larger than 70 only !\nEnter your password length: ")

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:
    password_gen_start()