PEP 8: Top-level functions should be surrounded by two blank lines. CamelCase names are usually for classes. Functions and variables should be in lower_case_with_underscores. Limit all lines to a maximum of 79 characters.
Instead of PasswordGenerator() in the end of your script write this:
if __name__ == '__main__':
PasswordGenerator()
You can read about it for example here.
3. If you have python3.6 then you can use f-strings:
GeneratePassword = input(f"{Name} would you like to generate a random password? ")
- You have some magic numbers in your code. How about taking them to function's signature? We also could take there
YesOptions and NoOptions and use type hints. After all, explicit is better than implicit.
- Instead of using
i variables in your loops use _. More information here.
sys.exit() is too extreme. Use return instead.
- Some lines of your code that go after
sys.exit() and break are unreachable.
- All this spaghetti code with recursions can be rewritten in a much clearer way. Check out this answer about asking user for a valid response.
In the end it can be something like this:
import random
import string
from typing import Set
def generate_passwords(*,
min_length: int = 8,
max_length: int = 16,
password_characters: str = (string.ascii_letters
+ string.digits
+ string.punctuation)
) -> None:
username = ask_name()
question = f'{username} would you like to generate a random password? '
while True:
if user_wants_password(question):
password_length = random.randint(min_length, max_length)
password = ''.join(random.choice(password_characters)
for _ in range(password_length))
print(password)
question = 'Would you like to generate another random password? '
continue
print('Good bye!')
return
def ask_name() -> str:
while True:
username = input('What is your name? ')
if not username.isalpha():
print('Not a valid response! Try again.')
continue
return username
def user_wants_password(question: str,
*,
yes_options: Set[str] = {'Yes', 'yes', 'Y', 'y'},
no_options: Set[str] = {'No', 'no', 'N', 'n'}) -> bool:
while True:
response = input(question)
if response in yes_options:
return True
elif response in no_options:
return False
else:
print('Not a valid response! Try again.')
if __name__ == '__main__':
generate_passwords()