Python Programming: Password Generator Project

Jul 30, 2024

Python Programming Lecture Notes - Password Generator Project

Previous Projects

  • Rock, Paper, Scissors Game
    • Simple game project discussed earlier.

Current Project Description

  • Project: Password Generator Program
  • Objective: Use knowledge gained so far (loops, random module, etc.) to create a password generator.
  • Levels:
    • Easy Level: Characters (letters, symbols, numbers) in a fixed order.
    • Hard Level: Characters shuffled for increased security.

Expected Output

  1. Welcome message: "Welcome to the Password Generator!"
  2. Prompt user for password components:
    • Number of letters
    • Number of symbols
    • Number of numbers
  3. Generate password according to user specifications:
    • Example: If 4 letters, 3 symbols, 2 numbers are requested, an example password could be eSn9%#().
    • Passwords can include:
      • Lowercase letters
      • Uppercase letters
      • Symbols
      • Numbers

Password Generation Logic

  • Importing Random Module: Use functions like random.choice and random.shuffle.
  • Lists Creation: Lists for letters (both cases), numbers, and symbols.
  • User Input: Take user input for the amount of each character type in the password.
  • Looping: Use loops to generate each component of the password.
  • String Concatenation: Combine generated characters into a single password string (easy level).
  • Shuffling (Hard Level): Use random.shuffle to shuffle the characters in the generated list for a strong password.

Implementation Steps

  1. Import Module: import random
  2. Create Lists:
    • Letters: letters = ['a', 'b', 'c', ..., 'A', 'B', 'C', ...]
    • Numbers: numbers = ['0', '1', '2', ..., '9']
    • Symbols: symbols = ['!', '@', '#', ...]
  3. Print Welcome Message:
    • print("Welcome to the Password Generator!")
  4. Take User Input:
    • Using input() and converting to integers.
      n_letters = int(input("How many letters? "))
      n_symbols = int(input("How many symbols? "))
      n_numbers = int(input("How many numbers? "))
      
  5. Generate Characters: Separate loops for letters, symbols, and numbers.
    • Example for letters:
      password = ""
      for _ in range(n_letters):
          password += random.choice(letters)
      
  6. Combine All Components: Append generated symbols and numbers.
    • Example:
      for _ in range(n_symbols):
          password += random.choice(symbols)
      for _ in range(n_numbers):
          password += random.choice(numbers)
      
  7. Shuffle for Hard Level:
    • Convert password string to list and shuffle:
      password_list = list(password)
      random.shuffle(password_list)
      password = ''.join(password_list)
      
  8. Print Final Password: Display the generated password:
    • print("Your password is: ", password)

Important Points to Remember

  • Understand logic before coding: Consider making a flowchart.
  • random.choice(): Selects random elements from a list.
  • random.shuffle(): Shuffles elements in a list.
  • String Concatenation: Use += for combining characters.
  • Use loops and range functions effectively.
  • Convert data types where needed (e.g., inputs to integers).

Try It Yourself

  • Easy Level: Generate passwords with components in a fixed order.
  • Hard Level: Shu""
  • Dry Run: Write down and trace through each line of code manually for understanding.