Simple Random Password Generator in Python I've made a password generator that works fine. It takes alphabetic, numeric, and punctuation characters and randomly generates a 8-16 character password.

import sys
import string
import random

def PasswordGenerator():
    Name = input("What is your name? ")
    if Name.isalpha():
        GeneratePassword = input(Name + " would you like to generate a random password? ")
        YesOptions = ["Yes", "yes", "Y", "y"]
        NoOptions = ["No", "no", "N", "n"]
        PasswordCharacters = string.ascii_letters + string.digits + string.punctuation
        while GeneratePassword in YesOptions:
            Password =  "".join(random.choice(PasswordCharacters) for i in range(random.randint(8, 16)))
            print(Password)
            GeneratePasswordAgain = input("Would you like to generate another random password? ")
            while GeneratePasswordAgain in YesOptions:
                Password =  "".join(random.choice(PasswordCharacters) for i in range(random.randint(8, 16)))
                print(Password)
                GeneratePasswordAgain = input("Would you like to generate another random password? ")
                break
            while GeneratePasswordAgain in NoOptions:
                print("Good bye!")
                sys.exit()
                break
            while GeneratePasswordAgain not in YesOptions or NoOptions:
                print("Not a valid response! Try again.")
                GeneratePasswordAgain = input("Would you like to generate another random password? ")
                break
        while GeneratePassword in NoOptions:
            print("Good bye!")
            sys.exit()
        while GeneratePassword not in YesOptions or NoOptions:
            print("Not a valid response! Try again.")
            PasswordGenerator()
            while GeneratePassword in YesOptions:
                Password =  "".join(random.choice(PasswordCharacters) for i in range(random.randint(8, 16)))
                print(Password)
                GeneratePasswordAgain = input("Would you like to generate another random password? ")
                while GeneratePasswordAgain in YesOptions:
                    Password =  "".join(random.choice(PasswordCharacters) for i in range(random.randint(8, 16)))
                    print(Password)
                    break
                    GeneratePasswordAgain = input("Would you like to generate another random password? ")
                while GeneratePasswordAgain in NoOptions:
                    print("Good bye!")
                    sys.exit()
                    break
                while GeneratePasswordAgain not in YesOptions or NoOptions:
                    print("Not a valid response! Try again.")
                    GeneratePasswordAgain = input("Would you like to generate another random password? ")
                    break
        while GeneratePassword in NoOptions:
            print("Good bye!")
            sys.exit()
    while Name is not Name.isalpha:
        print("Not a valid response! Try again.")
        PasswordGenerator()

PasswordGenerator()
I found a bug. If the initial response to "Do you want to..." is neither yes-like or no-like, it prompts for the username again. Also, you have large sections of dead code that will never execute.


Avoid duplicating code.

Several prompts are exactly the same as each other, even with the same logic after them. Refactor the code so that each possibility only occurs once.
Avoid duplicating variables

The GeneratePassword and GeneratePasswordAgain variables serve the same purpose: to get user input and determine if it should continue. and should be unified.
Keep the code reusable.

Avoid calls like sys.exit() that prevent this snippet of code from being used elsewhere.
Use the language features properly.

You know how to use while loops -- so why are you making multiple unnecessary recursive calls, each of which performs the same task as a while loop?
Separate logic and user-interface. Separate the code into modules.

Write the function that generates the password and does nothing else. Something like

 def RandomPassword(passwordCharacters, minLetters, maxLetters):
    return "".join(random.choice(PasswordCharacters) for i in range(random.randint(minLetters, maxLetters)))

 def DefaultRandomPassword():
     passwordCharacters = string.ascii_letters + string.digits + string.punctuation
     return RandomPassword(passwordCharacters, 8, 16)


Write another function that takes a prompt, displays it, waits for the user to type something, and then does it again if it was not a valid yes-no answer. Then return either yes or no. Call this function multiple times.