- Day 5 Goals
- For Loop
- sum()
- range() function
- Coding Challenge: FizzBuzz
- Project: Password Generator
My fizzbuzz challenge
# You are going to write a program that prints the solution to the FizzBuzz game.
#These are the rules of the FizzBuzz game:
# Your program should print each number from 1 to 100 in turn and include number 100.
# when the number is divisible by 3 then it should print "Fizz".
# when the number is divisible by 5, then it should print "Buzz".`
# and if the number is divisible by both 3 and 5 e.g. 15 then it should print "FizzBuzz"
for i in range(1,101):
if (i % 3 == 0) and (i % 5 == 0):
print("FizzBuzz")
elif i % 5 ==0:
print("Buzz")
elif i % 3 == 0:
print("Fizz")
else:
print(i)
My project:
Password Generator (Letter, Symbols, Numbers in sequence)
# create a random password using the lists of letters, numbers and symbols
# ask the user how many of each they want
# print the generated password
import random
from email.contentmanager import raw_data_manager
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"))
# with no randomisation of the character places
pwd = ""
for nrl in range(0, nr_letters):
i = random.randint(0, len(letters) - 1)
pwd = pwd + letters[i]
for nrs in range(0, nr_symbols):
i = random.randint(0, len(numbers) - 1)
pwd = pwd + symbols[i]
for nrn in range(0, nr_numbers):
i = random.randint(0, len(symbols) - 1)
pwd = pwd + numbers[i]
print(f"Your simple password is {pwd}")
Password Generator (Randomised Characters)
# create a random password using the lists of letters, numbers and symbols
# ask the user how many of each they want
# randomise the position of each character.
# print the generated password
import random
from email.contentmanager import raw_data_manager
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"))
# with place randomisation
pwd_lst = []
for nrl in range(0, nr_letters):
i = random.randint(0, len(letters) - 1)
pwd_lst.append(letters[i])
for nrs in range(0, nr_symbols):
i = random.randint(0, len(symbols) - 1)
pwd_lst.append(symbols[i])
for nrn in range(0, nr_numbers):
i = random.randint(0, len(symbols) - 1)
pwd_lst.append(numbers[i])
# now we have selected random number letters and symbols we need to randomise them in the list
pwd_str = ""
random.shuffle(pwd_lst) # this uses the random module to randomise the list
# and now convert the list to a string - here I am doing it using only code we have used so far
for i in range(0, len(pwd_lst)):
pwd_str += pwd_lst[i] # iterate through the list, take each character and concatenate it to a string
print(f"your random password is {pwd_str}")