100 Days of Code: Day 5 - Password Generator
Challenge: 100 Days of Code with Python
Day: 5
Introduction
Welcome to Day 5 of my 100 Days of Code challenge with Python! Today, I created a powerful and essential tool for cybersecurity: a Password Generator. This project generates strong and secure passwords by combining letters, numbers, and symbols. It's an excellent way to practice working with lists, loops, and the random module in Python.
Project: Password Generator
The Password Generator allows users to specify the number of letters, symbols, and numbers they want in their password. The program then creates a random password with these parameters, ensuring it's both strong and secure.
The Code
Here is the Python code for the Password Generator with added ASCII art and updated wording to be more cybersecurity-focused:
# Password Generator Project
import random
# ASCII Art
print("""
____ _ _____
/ __ \ | | | __ \
| | | |_ __ __ _ __ _ ___ _ __ ___ __| | | |__) | __ _____ ___ _
| | | | '__/ _` |/ _` |/ _ \| '__/ _ \ / _` | | ___/ '__/ _ \ \/ / | | |
| |__| | | | (_| | (_| | (_) | | | (_) | (_| | | | | | | (_) > <| |_| |
\____/|_| \__,_|\__, |\___/|_| \___/ \__,_| |_| |_| \___/_/\_\\__, |
__/ | __/ |
|___/ |___/
""")
# Lists of characters to be used in the password
letters = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'
]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
# Welcome message
print("Welcome to the PyPassword Generator! Your secure password awaits. 🔒")
# User input for password requirements
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input("How many symbols would you like?\n"))
nr_numbers = int(input("How many numbers would you like?\n"))
# Hard Level - Randomly select characters and shuffle them
password_list = []
# Add random letters to the password list
for char in range(1, nr_letters + 1):
password_list.append(random.choice(letters))
# Add random symbols to the password list
for char in range(1, nr_symbols + 1):
password_list.append(random.choice(symbols))
# Add random numbers to the password list
for char in range(1, nr_numbers + 1):
password_list.append(random.choice(numbers))
# Debug: Print the list before shuffling
print(f"Password list before shuffling: {password_list}")
# Shuffle the password list to ensure randomness
random.shuffle(password_list)
# Debug: Print the list after shuffling
print(f"Password list after shuffling: {password_list}")
# Combine the list items into a single string
password = ""
for char in password_list:
password += char
# Display the final password
print(f"Your secure password is: {password}")
Key Functions Explained
1. ASCII Art and Welcome Message
The program starts with an ASCII art banner and a welcome message to make the user experience more engaging.
print("""
____ _ _____
/ __ \ | | | __ \
| | | |_ __ __ _ __ _ ___ _ __ ___ __| | | |__) | __ _____ ___ _
| | | | '__/ _` |/ _` |/ _ \| '__/ _ \ / _` | | ___/ '__/ _ \ \/ / | | |
| |__| | | | (_| | (_| | (_) | | | (_) | (_| | | | | | | (_) > <| |_| |
\____/|_| \__,_|\__, |\___/|_| \___/ \__,_| |_| |_| \___/_/\_\\__, |
__/ | __/ |
|___/ |___/
""")
2. User Input Handling
The program asks the user for the number of letters, symbols, and numbers they want in their password.
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input("How many symbols would you like?\n"))
nr_numbers = int(input("How many numbers would you like?\n"))
3. Generating the Password
The program generates the password by randomly selecting the specified number of letters, symbols, and numbers. It then shuffles these characters to ensure randomness.
password_list = []
# Add random letters to the password list
for char in range(1, nr_letters + 1):
password_list.append(random.choice(letters))
# Add random symbols to the password list
for char in range(1, nr_symbols + 1):
password_list.append(random.choice(symbols))
# Add random numbers to the password list
for char in range(1, nr_numbers + 1):
password_list.append(random.choice(numbers))
# Debug: Print the list before shuffling
print(f"Password list before shuffling: {password_list}")
# Shuffle the password list to ensure randomness
random.shuffle(password_list)
# Debug: Print the list after shuffling
print(f"Password list after shuffling: {password_list}")
4. Creating the Final Password
The shuffled list of characters is then combined into a single string, which is the final password.
password = ""
for char in password_list:
password += char
# Display the final password
print(f"Your secure password is: {password}")
Conclusion
This Password Generator project was a fantastic way to practice working with lists, loops, and the random module in Python. It also emphasizes the importance of strong and secure passwords in cybersecurity. Stay tuned for more exciting projects in the upcoming days!
Stay Connected
Follow my progress and join me on this coding journey:
Happy coding!