Skip to main content
deleted 30 characters in body
Source Link
Georgy
  • 2k
  • 2
  • 15
  • 27
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():
            return username
        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()
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()
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 username.isalpha():
            return username
        print('Not a valid response! Try again.')


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()
Source Link
Georgy
  • 2k
  • 2
  • 15
  • 27

  1. 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.

  2. 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? ")
  1. 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.
  2. Instead of using i variables in your loops use _. More information here.
  3. sys.exit() is too extreme. Use return instead.
  4. Some lines of your code that go after sys.exit() and break are unreachable.
  5. 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()