Python Password Generator

Python Password Generator

·

2 min read

#100-days-of-code

So, finishing the 5th day of '100 days of code' in Python (yes, I am a beginner), I learnt how to build a simple password generator in Python using 'for loop', 'random.shuffle()', and 'random.choice()' function. Based on the number of letters, symbols, and letters (uppercase and lowercase) that the user specifies, it outputs a string.

Good practice. Here is the code:

import random
#we have the letters , numbers and symbols here

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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

#these are the questions

nr_letters= int(input("How many letters would you like in your password?\n")) 
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

#Here starts the code, we put random items into a pass_list and increment it

pass_list=[]
for i in range(0, nr_letters):
    pass_list+=random.choice(letters)
for i in range(0, nr_numbers):
    pass_list+=random.choice(numbers)
for i in range(0, nr_symbols):
    pass_list+=random.choice(symbols)

#to reorder the list items randomly we use this function:

random.shuffle(pass_list)

#we turn the array into a string

password=""
for i in pass_list:
    password+=i

print(password)