Problem overview
Version 1 of the password strength checker looks at one thing only: how many characters the password has. The starting code sets password = "Secret99". The program must report the length and then give a verdict: OK for 8 characters or more, Too short otherwise. The output is collected in a string named receipt, with the lines separated by newline characters. The lesson shows receipt for you, so the code doesn't call print.
For "Secret99" the receipt is:
Password length: 8
OK
For "abc" it is Password length: 3 followed by Too short.
Approach: measure once, use twice
The length appears in the first output line and in the comparison with 8, so it is computed once with len(password) and stored in length. The lines are collected in a list named lines. The verdict is a choice between two outcomes, which is an if/else. A final "\n".join(lines) turns the list into the single string the lesson expects.
Step by step
- Create
length.len(password)counts the characters. - Create the empty list
lines. The output lines are added to it one at a time. - Append the first line. The label
Password length:followed bylengthturned into text withstr(). The label ends with a space, so the number is separated from the colon. - Write the
if. Whenlength >= 8, appendOK. The comparison includes 8, so a password of exactly 8 characters is accepted. - Write the
else. It appendsToo shortfor every length theifdidn't take, which is 7 or fewer. - Create
receipt."\n".join(lines)glues the lines together with a newline between each two.
The solution in Python
password = "Secret99"
length = len(password)
lines = []
lines.append("Password length: " + str(length))
if length >= 8:
lines.append("OK")
else:
lines.append("Too short")
receipt = "\n".join(lines)
Details worth knowing:
lencounts characters. A space is a character like any other, so a password made of eight spaces has length 8 and getsOK. This version doesn't judge what the characters are.str()is required."Password length: " + lengthraisesTypeError: can only concatenate str (not "int") to str, because+won't join text and an integer.joingoes on the separator. The string before.joinis placed between the elements. With the two lines of this lesson, the receipt is'Password length: 8\nOK', with no newline at the end.- Exactly one branch runs. The
ifand theelsetogether always append exactly one verdict line.
Edge cases and common mistakes
- The boundary. A password of 8 characters gives
OKand one of 7 givesToo short. Using>instead of>=would reject the 8-character one. - The empty password. With
password = "", the length is 0 and the receipt isPassword length: 0followed byToo short. - Calling
print. The lesson readsreceipt, so the program only has to build it. - Appending the verdict outside the branches. Appending
OKunconditionally would put it in every receipt. - Forgetting the join.
receiptwould be a list, not a string. - Joining with
""or" ". The lines would sit on one row instead of two.
Testing
"Secret99"gives length 8 andOK."abc"gives length 3 andToo short."abcdefg"gives length 7 andToo short."abcdefgh"gives length 8 andOK.""gives length 0 andToo short.
