DEV Community

John Mark Rafanan
John Mark Rafanan

Posted on

Day 05 of 100 Days of Code Python Bootcamp by Dr. Angela

Day 05 - For Loops, Range and Code Blocks

For this day, I created a Password generator using For Loops and random.shuffle() method


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

print("Welcome to the PyPassword Generator!")
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"))

#Order of characters randomised:
#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P
password = ""
random_password = ""

for i in range (0, nr_letters): #loop through letters using range of input
  letters_index = random.randint(0,len(letters)-1) #get random item on letters
  password += letters[letters_index]

for i in range (0, nr_symbols): #loop through symbols using range of input
  symbol_index = random.randint(0, len(symbols)-1) #get random item on symbols
  password += symbols[symbol_index]

for i in range (0, nr_numbers): #loop through numbers using range of input
  number_index = random.randint(0, len(numbers)-1) #get random item on numbers
  password += numbers[number_index]

splitted_pass = list(password)
random.shuffle(splitted_pass)

for word in splitted_pass:
    random_password += str(word)
print(random_password)

Enter fullscreen mode Exit fullscreen mode

Output:

Day 05 Output

Top comments (0)