Project 5 of 13, for Aaesha
Password Checker
Six rules for the server that holds Aaesha's experiment results, and a reason for every one a password breaks.
No account needed. Your work is kept in this browser until you sign up.
The brief
- AaeshaMy memory experiments run on the class server, and everyone's results are on there.
- AaeshaLast year every account had the password clocktower1. Four people knew it, and then everyone did.
- AaeshaPeople pick passwords they can remember, and that's exactly what makes them easy to guess.
- AaeshaI've got six numbered rules, and when it says no it has to say why.
- PaulOh, numbered requirements, every programmer's favourite kind.
Requirements, from Aaesha. Numbered, naturally.
Ask for a username (Username? ) and a password (Password? ). For every rule the password breaks, print the reason, in this order:
- At least 10 characters.
Too short: at least 10 characters. - At least one capital and one lower-case letter.
Needs capitals and lower case. - Not only letters.
Needs a number or a symbol. - The username isn't inside it, in any case.
Contains your username. - The word
passwordisn't inside it, in any case, even with a0for theo.Contains the word password. - It doesn't end in four digits.
Ends in a year.
The last line is Password accepted. if it broke no rules, and Password rejected. if it broke any.
Username? paul Password? Clock0314 Too short: at least 10 characters. Ends in a year. Password rejected.
What you learn
The lesson, step by step
Looking inside text
inasks whether one piece of text appears anywhere inside another."tower" in "Clocktower"isTrue, and"bell" in "Clocktower"isFalse. Like a comparison, it always answers with a bool. It says whether, never where.not inasks the opposite question.A password checker leans on it. Is the username inside the password? Is the word
password?Capitals count. To Python,
Candcare different characters, so"clock" in "Clocktower"isFalse. The usual mistake is expectinginto match whatever the capitals. The next Learn step shows how to ignore them.Is it in there
Last year, every account on the class server had
clocktower1as its password. Aaesha keeps her experiment results there, and she would like that stopped.Strings answer questions
A string comes with methods: small tools you call with a dot, a name and brackets, like
password.lower(). Leave the brackets off and the method doesn't run..lower()hands back a copy in lower case, and.upper()a copy in capitals. That's how to ignore capitals:"clock" in password.lower()looks in a lower-case copy.Methods whose names start with
isanswer with a bool..isalpha()isTrueonly if every character is a letter,.isdigit()only if every character is a digit, and.isupper()only if every letter is a capital."Every" is the usual catch. One capital isn't enough for
.isupper(), and one digit makes.isalpha()False.Counting characters
len()counts the characters in a string: letters, digits, spaces, everything.len("Clock0314")is9.Each character has a position, counted from 0. So with
room = "Workshop2019",room[0]isW. Negative positions count back from the end:room[-1]is9, the last character.A slice takes a run of characters:
room[0:4]isWork. It stops just before its second number. Leave a number out to run to the edge:room[:4]is the first four, androom[-4:]the last four,2019.A single position past the end, like
room[12], stops with an IndexError. A slice past the end just stops at the end.The usual mistake is counting from 1. The first character is at position 0.
Asking the password questions
Aaesha's least favourite password, and some questions to ask it. Each one uses a method or a slice from the last two steps.
A few more methods
The password checker needs only a handful of methods, but strings have plenty more. Four that come up often:
.title()gives a copy with a capital at the start of each word:"the puzzle room".title()isThe Puzzle Room..count("o")counts how many times"o"appears: twice inClocktower..startswith("Work")isTrueif the string begins withWork, capitals and all..center(6, "-")pads the string to 6 characters in all, with the text in the middle:--ab--. Without the"-", it pads with spaces.
The usual mistake with
.center()is reading the number as how much to add on each side. It's the total width.Tidying text
People type spaces before and after things without noticing.
.strip()hands back a copy with the spaces taken off both ends:" Yahya ".strip()is"Yahya". It never touches spaces in the middle. Give it characters, as in.strip(".!"), and it takes those off the ends instead. On the right, line 2 adds a|so you can see where the stripped text ends..replace(old, new)hands back a copy with everyoldswapped fornew:"passw0rd".replace("0", "o")is"password". Every match, not just the first.You can call one method straight after another.
" Yahya ".strip().lower()strips first, then lowers the stripped copy:"yahya".The usual mistake is expecting
.strip()to take out every space, including the ones between words.Strings can't be changed
Once a string exists, it can't be changed. Methods like
.upper(),.strip()and.replace()hand back a new string, and leave the old one as it was.So line 2 of the example does nothing useful.
name.upper()makes"RIYA"and hands it back, but nobody keeps it, sonamestill holds"riya".To keep the new string, move the label onto it with
=, as line 4 does:name = name.upper(). You can't change one character, either:name[0] = "R"stops with a TypeError.The usual mistake is calling a method and expecting the variable to change. It never does. Python doesn't complain, either: it works out the answer, and throws it away.
Tidying the username
Two spaces before
Yahyaand one after: 8 characters in all. Line 2 strips them, or tries to.and, or, not
and,orandnotjoin bools together:a and bisTrueonly when both sides areTrue.a or bisTruewhen at least one side is, including when both are.not aturnsTrueintoFalse, andFalseintoTrue.
So with
hour = 20,hour < 7 or hour >= 19isTrue, because the second side is.Each side has to be a whole question. The usual mistake is
choice == "y" or "yes". It reads well, but Python doesn't share thechoice ==between the two sides. The right side is just the string"yes", and Python counts any string that isn't empty as true, so the whole thing is alwaysTrue.Either one
Someone wrote this test for last year's accounts, to catch the two most common passwords.
Chalk-Dust-42is neither of them.How Paul would do it
- PaulAaesha asked for usernames first, so I did those. It's the same shape with fewer rules.
A program like the password checker: Paul's rules for usernames. Three parts to look at closely:
- Line 3,
name.strip().lower(), tidies what was typed: strip first, then lower case. okis a bool that startsTrue. Every rule that fails prints its reason and setsoktoFalse, and nothing sets it back..isalnum()isTruewhen every character is a letter or a number. Line 10 takes the dots out of a copy first, because dots are allowed.
The four labels beside the code are Paul's steps. Your file for the Build starts with the same four, as comments.
Fix Aaesha's version
- AaeshaI wrote the letters rule first and checked each half of it on its own, and both were right.
- AaeshaThen I typed clocktower1987 and it said the letters were fine, which they aren't. Can you look?
Aaesha's version checks only the letters rule: a password needs at least one capital and at least one lower-case letter.
has_capitalandhas_lowereach hold a bool, the answer to a question, kept for later.Run it and type
clocktower1987. It should printNeeds capitals and lower case.Work out why it doesn't, then fix it.Make the password checker
Build the checker to Aaesha's six rules, listed at the top of your file. For every rule the password breaks, print its reason, in the order of the rules. Then print the verdict.
Username? paul Password? Clock0314 Too short: at least 10 characters. Ends in a year. Password rejected.Your file also has Paul's four steps from the usernames program, as comments. Tidy the username with
.strip()and.lower(), and look for it in a lower-case copy of the password. Don't strip the password: a space in a password is its owner's business. Give each rule its ownif, so every reason gets printed.The checks try passwords that break each rule, several at once, and edge cases like exactly 10 characters, or two digits on the end.
Ship it
- AaeshaClock0314 is too short and ends in four digits, and it told Paul both of those. Quite right too.
- PaulIn my defence, I only typed that one to test it.
- AaeshaHe's chosen a new one now and he won't tell me what it is. Also correct, as it happens.