DEV Community

Cover image for Python 23: Password Generator
Gregor Schafroth
Gregor Schafroth

Posted on

Python 23: Password Generator

Today ChatGPT suggested me to make a password generator. First I share the exercise, then my solution ๐Ÿ™‚

Exercise: Password Generator

Write a Python program that generates random passwords based on user-defined criteria. The program should perform the following steps:

  1. Ask the user for the desired length of the password (e.g., 8 characters, 12 characters, etc.).
  2. Ask the user if they want to include uppercase letters, lowercase letters, digits, and special characters in the password (e.g., "Yes" or "No" for each category).
  3. Generate a random password that meets the user's criteria and display it.

My Solution

I coded most of it myself, but used AI help for make_pw(), as I think I am fine without learning the random and string libraries in detail.


import logging
import random
import string

logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s: %(message)s')

# Determines the length of the password
def get_length():
    while True:
        user_input = input('How many characters should the password have? ')
        try:
            user_number = int(user_input)
            return user_number
        except ValueError:
            logging.error('Please answer with a number')

# Determines what characters are to be used in the password
def get_characters():
    characters = {}

    while True:
        lower_case = input('Do you want to include lower case letters? ')
        if lower_case == 'y' or lower_case == 'yes':
            characters['lower_case'] = 1
            break
        elif lower_case == 'n' or lower_case == 'no':
            characters['lower_case'] = 0
            break
        logging.error('Please answer with "yes" or "no"')

    while True:
        lower_case = input('Do you want to include upper case letters? ')
        if lower_case == 'y' or lower_case == 'yes':
            characters['upper_case'] = 1
            break
        elif lower_case == 'n' or lower_case == 'no':
            characters['upper_case'] = 0
            break
        logging.error('Please answer with "yes" or "no"')

    while True:
        lower_case = input('Do you want to include digits? ')
        if lower_case == 'y' or lower_case == 'yes':
            characters['digits'] = 1
            break
        elif lower_case == 'n' or lower_case == 'no':
            characters['digits'] = 0
            break
        logging.error('Please answer with "yes" or "no"')

    while True:
        lower_case = input('Do you want to include special characters? ')
        if lower_case == 'y' or lower_case == 'yes':
            characters['special_characters'] = 1
            break
        elif lower_case == 'n' or lower_case == 'no':
            characters['special_characters'] = 0
            break
        logging.error('Please answer with "yes" or "no"')

    return characters

# Generates the password based on input criteria
def make_pw(pw_length, pw_characters):
    selected_characters = ''

    if pw_characters.get('lower_case'):
        selected_characters += string.ascii_lowercase
    if pw_characters.get('upper_case'):
        selected_characters += string.ascii_uppercase
    if pw_characters.get('digits'):
        selected_characters += string.digits
    if pw_characters.get('special_characters'):
        selected_characters += string.punctuation

    if not selected_characters:
        return "No characters selected."

    password = ''.join(random.choice(selected_characters) for _ in range(pw_length))
    return password

def main():
    print('This program will generate a password for you. Please answer the following questions to customize your password.')
    pw_length = get_length()
    pw_characters = get_characters()
    pw = make_pw(pw_length, pw_characters)
    print(pw)

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)