In this project, you’ll write two programs: a password generator and a password checker.
Write a program that prints a randomly generated password like “Fj3io19aA” to the screen and then exits. Every time you run the program, it’ll print out a different password, like this:
You already know that you can print something to the screen by writing code like
print("Hello there!"), but we’ll need to do some thinking if we want to figure out how to actually generate a password from scratch. I’ll give you a few useful bits of code that might come in handy.
Here’s how to use the
choice() function from the random library to choose a random lower-case letter:
import random lowercase_letters = 'abcdefghijklmnopqrstuvwxyz' random_letter = random.choice(lowercase_letters) print(random_letter)
The code snippet above is interactive, which means:
Ctrl+Enter(hold down the Control key, then press the Enter key). Try doing that now. Notice that it prints out a different letter almost every time you run it!
lowercase_letters = 'ABCDE'and see what happens. (NOTE: calling the variable
lowercase_lettersdoesn’t automatically force it to hold only lowercase letters!) Press
Ctrl+Entera few more times while you’re at it.
When we give you assignments in this class, they’ll often have interactive code snippets like these because you can learn a lot by playing around with the example code yourself instead of just reading it. You should mess around with every one of these code snippets yourself; it makes the whole experience less intimidating, and it’s really fun!
OK, so we know how to pick a random lowercase letter. You can also use the same approach in order to pick a random uppercase letter, or number, or symbol.
Another useful thing to remember is that you can add strings together (we call this “concatenating” them) by using the
a_lowercase_letter = "j" an_uppercase_letter = "N" a_number = "6" print(a_lowercase_letter + an_uppercase_letter + a_number)
Remember that you can check the length of a string by calling the
Finally, here’s how to use the
sample() function from the
random library to shuffle a string:
import random my_name = 'JR Heard' shuffled_name = ''.join(random.sample(my_name, len(my_name))) print(shuffled_name)
my_name to be your name so you’re sure that this code works on your name too, and don’t forget to use
Ctrl+Enter to re-execute the snippet a bunch of times until you’re convinced it’s different each time.
There’s some weird stuff going on in that snippet - what’s that
''.join() call all about, for instance? - but I’m not going to explain it just yet, we’ll cover it in a later assignment. For now, you can just copy-paste that line of code into your program if you’d like to use it. In later projects, you won’t be allowed to copy-paste code you don’t understand, so be sure to cherish this moment while it lasts.
Now you have everything you’ll need in order to write a password generator!
Your generator should generate passwords that meet the PPS standard: they should be at least 8 characters long, and they should include at least 3 of these 4 categories: number, uppercase letter, lowercase letter, symbol. (A “symbol” is one of these:
Write a program that asks the user for a password and prints out
"GOOD" if it meets the PPS standard mentioned above, or
"BAD" if the password does not meet the PPS standard. Remember that you can use
input() to ask the user for a password.
In addition to the requirements mentioned above, passwords should not contain your username or your student ID. At the start of your program, ask the user for their username and student ID so that you can check to make sure that those things aren’t in their password.
When it’s done, your password checker should behave just like this:
In order to check that a password meets the PPS criteria, you’ll want to loop over each character of the password and write some code that keeps track of whether it has any lowercase letters, uppercase letters, symbols, or numbers. For instance, here’s a bit of code that checks to see how many times the letter
"z" is in the word
word = "Pizza" number_of_zs = 0 for letter in word: if letter == 'z': number_of_zs = number_of_zs + 1 print(number_of_zs)
The above snippet teaches you how to loop over every character of a string —
'a' — and do something based on the value of that character. Remember, though, that we don’t care about how many uppercase letters are in a password; we just care about whether or not there are any.
You can check to see if one string is in another string by using Python’s
in operator, like this:
# `in` works for single letters like 'z' and 'f'... print('z' in 'Pizza') print('f' in 'Pizza') # ...and also for longer strings like 'llo' and 'potatoes'. print('llo' in 'Hello') print('potatoes' in 'Hello')
Notice that you get a result of
False, so you could use this code as a condition in an
If my username is
jrheard and my student ID is
12345, then per the rules mentioned above, these are bad passwords:
Here’s one slightly tricky thing about this part of the project: my username is
"jrheard", and the password
"CarlsjRHeard!" is invalid, but Python strings are case sensitive:
print("jrheard" == "jRHeard")
Your password checker should be able to tell if a password contains your username, even if the password’s capitalization is all funky like that. One way to handle this problem is to call a string’s
.upper() method, like this:
As you’re working on your program, you might find it useful to add some extra
print() calls that print out what programmers call “debug information” to help you understand what your program’s actually doing. For instance:
It’s OK if you leave those
print() calls in there, you don’t need to remove them before submitting your project. Just be sure that the last line of your checker’s output says the word
GOOD or the word
BAD, with nothing else on that line, like you see in the example above.
That should be everything you need to get started. Good luck!
Submit two files:
On the first line of each of those two files, write a comment with your name on it, like this:
# JR Heard
Remember to follow this class’s style guide.
The part about descriptive variable names is really important! For instance:
nis a bad variable name,
usernameis a good one.
nsis a bad variable name,
number_of_symbolsis a good one.