Python Project: Password Generator Project

Python Project: Password Generator Project

Introduction:


In this project, I created a Password Generator using Python. The program allows users to generate secure passwords of custom lengths while validating the input to ensure correctness. It includes uppercase and lowercase letters, digits, and special characters, making it a great way to understand string manipulation, loops, and input validation.

These are the steps I took to grasp the concepts:


  1. Understanding the Problem:

    • I researched how a password generator should work and the key requirements for strong passwords.

    • So I learned about the string module in Python from its documentation for generating character pools and the random module for shuffling elements.

  2. Character Pool Setup:

    • I used string.ascii_uppercase, string.ascii_lowercase, string.punctuation, and string.digits to include all necessary characters for the password.
  3. Password Generation:

    • The next step is to combine the character pool into a single list, shuffle it, and slice it based on the user-specified length to generate the password.
  4. Input Validation:

    • Also, it’s very important to implement a loop to ensure the user provides a valid input (a positive integer and within the character pool limit).
  5. Below is the complete Python code :

These are the problems that I encountered:


  1. Fixing the None Error with Python's extend() Method in List Manipulation

  2. Invalid User Input for Password Length.

  3. Password Length Exceeding the Character Pool.

This is how I solved those problems:


1. Fixing the None Error with Python's extend() Method in List Manipulation:

Problem: While working with Python’s extend() method to combine multiple lists, I encountered an issue where my list was replaced with None. This happened because the extend() method in Python modifies the original list directly but does not return the modified list. Instead, it returns None.

s = []  
s = s.extend(list(s1) + list(s2) + list(s3) + list(s4))  # The error: s is now None  
print(s)  # Output:None (instead of the combined list)

Solution: To fix this, I simply removed the assignment to s. The extend() method changes the list directly, so there’s no need to assign the result back to s.

s = []  
s.extend(list(s1) + list(s2) + list(s3) + list(s4))  # Correct way to use extend  
print(s)  # Now it prints the correct combined list

2. Invalid User Input for Password Length:

Problem: The program crashed when the user entered alphabets or special characters instead of a number for the password length. Additionally, it failed to handle negative numbers.

# Code without Handling Invalid User Input:
import string
import random
s1 = string.ascii_uppercase
s2 = string.ascii_lowercase
s3 = string.punctuation
s4 = string.digits
s = []
s.extend(list(s1) + list(s2) + list(s3) + list(s4))
random.shuffle(s)
passlen = input("Enter the length of your password: ")
print("Your Generated Password:")
print("".join(s[0:passlen]))

Solution: To resolve this, I implemented input validation as follows:

  • Used the str.isdigit() method to ensure the input is numeric.

  • Added a condition to reject negative numbers and again ask the user for valid input.

while True:
    passlen = input("Enter the length of your password: ")
    if not passlen.isdigit(): # Check if input is numeric
        print("Invalid Input. Please enter a positive integer!")
        continue
    passlen = int(passlen)    # Convert to integer after validation
    if passlen < 0:           # Check if input is non-negative
        print("Invalid Input. Length cannot be negative. Please try again!")
        continue
    break                     # Exit the loop if input is valid

3. Password Length Exceeding the Character Pool:

Problem: When users entered a length greater than the size of the character pool, the program generated a password with the maximum available characters (94), instead of the requested length. This could confuse users expecting a longer password.

Solution: To handle this, I added a length restriction check as follows:

  • Calculated the maximum allowable length using the size of the character pool (len(s)).

  • Compare the user input with this maximum limit.

  • If the requested length exceeded the limit, the program displayed an error message and prompted the user to input a valid length again.

# Maximum allowable length
max_length = len(s)

while True:
    passlen = input("Enter the length of your password: ")
    if not passlen.isdigit():
        print("Invalid Input. Please enter a positive integer!")
        continue
    passlen = int(passlen)
    if passlen < 0:
        print("Invalid Input. Length cannot be negative. Please try again!")
        continue    
    if passlen > max_length: # Check if length exceeds maximum limit
        print(f"Password length exceeds the maximum limit ({max_length}). Please try again!")
        continue
    break

Output when running in the terminal:


Example 1: Valid Input

Enter the length of your password: 9
Your Generated Password:
Hz~_tTR4p

Example 2: Invalid Input

Enter the length of your password: abc
Invalid Input. Please enter a positive integer!
Enter the length of your password: -8
Invalid Input. Please enter a positive integer!
Enter the length of your password: 0
Invalid Input. Length cannot be negative. Please try again!

Example 3: Length Exceeding Limit

Enter the length of your password: 100
Password length exceeds the maximum limit (94). Please try again!

Resources: