Project 2
Password Generator
The Project
For this project, you’ll ues IDLE to write a program that prints a randomly generated password like “Fj3io19aA” to the screen and then exits. Every time you run the program, it will print out a different password.
Note
In reality, the passwords generated in this project aren’t all that secure.
Starter Code
Start by downloading this program’s starter code and opening it in IDLE.
Note that the starter code has a generate_password()
function. Write your code in that function, and don’t change the function’s name.
The starter code also has some comments with the word TODO in them, which are just reminders about what you’re supposed to do. Delete those TODO comments once you’ve done the things that they tell you to do.
Random Choice
Python has a choice()
function in Python's random
library that we can use to choose a random letter:
from random import choice
lowercase_letters = 'abcdefghijklmnopqrstuvwxyz'
random_letter = choice(lowercase_letters)
# random_letter is now a single (random) letter chosen from lowercase_letters
You can use the same approach in order to pick a random uppercase letter, or number, or symbol. Go ahead and try it out!
Randomizing the order of letters
There is another function called sample()
in the random
library that we can use to shuffle a string:
from random import sample
my_name = 'JR Heard'
length = len(my_name)
shuffled_name = ''.join(sample(my_name, length))
print(shuffled_name)
# example output: Rd aHerJ
There’s some weird stuff going on in that snippet - what’s that ''.join()
call all about, for instance? - but for now, you can just use the code in your program. Try it!
Requirements
Now you have everything you’ll need in order to write a password generator!
Each password should:
- be at least 8 characters long.
- include at least 3 of these 4 categories:
- number
- uppercase letter
- lowercase letter
- symbol -- for this project, a “symbol” is one of these:
!@#$%^&*()-_=+,.
- not have a predictable pattern
- not have an intentional repetition of characters
(This standard was partly modeled after the PPS requirements.)
Submitting your project
Before submitting your project, make sure to test your program a bunch of times to make sure your password generator meets this standard! Also remember to follow this class’s style guide.
On Google Classroom, submit your program in a file called password_generator_YOUR_NAME.py
. For example, I’d submit a file called password_generator_TAMARA_OMALLEY.py
.
On the first line of that file, double check that you wrote a comment with your name on it, like this:
# Tamara O'Malley
The projects in this class were created with a LOT of help from JR Heard, a TEALS volunteer at Madison, 2017-2019. His version of this project lives at https://blog.jrheard.com/python/password-generator .