Problem overview
Version 5 changes the question again. Instead of looking at the characters, it asks whether the password is on a list of well-known weak ones. The starting code defines a dictionary common with four entries, "password", "123456", "qwerty" and "letmein", each mapped to True, and sets password = "qwerty". The receipt in receipt has three lines: the password, whether it is common, and a verdict.
Password: qwerty
Common: True
Weak: reject
For "Tr0ub4dor" the lines are Password: Tr0ub4dor, Common: False and Strong: accept.
Approach: a dictionary as a lookup table
Checking a password against a list of bad ones could mean comparing it with each entry in turn. A dictionary does the search differently: it finds a key directly, so the time doesn't grow with the number of entries. The values in this dictionary, all True, hold no information. Only the keys matter. A set of the same four strings would work the same way.
The in operator asks a dictionary whether a value is one of its keys, and it answers with a boolean. That boolean is stored in is_common, shown on the second line, and used for the verdict.
Step by step
- Create the empty list
lines. - Append the first line.
Password:followed bypassword. The password is already text, so it needs nostr(). - Create
is_common.password in commonisTruewhen the password is one of the dictionary's keys. - Append the second line.
Common:followed bystr(is_common). A boolean has to be turned into text first. - Write the
if. Whenis_commonis true, appendWeak: reject. - Write the
else. Otherwise appendStrong: accept. - Create
receipt."\n".join(lines).
The solution in Python
common = {"password": True, "123456": True, "qwerty": True, "letmein": True}
password = "qwerty"
lines = []
lines.append("Password: " + password)
is_common = password in common
lines.append("Common: " + str(is_common))
if is_common:
lines.append("Weak: reject")
else:
lines.append("Strong: accept")
receipt = "\n".join(lines)
Details worth knowing:
inchecks keys. For a dictionary,x in commonlooks at the keys and ignores the values. If the dictionary held"qwerty": False, the password would still count as common.- Booleans in the condition.
if is_common:already tests a boolean, soif is_common == Trueadds nothing. - Case counts. The comparison is exact. "qwerty" is common but "QWERTY" is not, and neither is "Qwerty". A real checker would lower-case the password before the lookup, but this lesson matches the text exactly as given.
- Strong here means "not on the list". The verdict says nothing about length or character kinds. "Tr0ub4dor" is accepted because it isn't in the table, and so is the empty string.
- The
elseis skipped for the starting password. "qwerty" is a key, so only theifbranch runs.
Edge cases and common mistakes
- Another common password. "password" gives
Common: TrueandWeak: reject. - A different case. "QWERTY" gives
Common: FalseandStrong: accept. - The empty password. It is not a key, so the lines are
Password:(with nothing after it),Common: FalseandStrong: accept. - Writing
common[password]. For a password that isn't in the table it raises aKeyError. Theinoperator never raises. - Concatenating the boolean.
"Common: " + is_commonraises aTypeError. Usestr(is_common). - Testing the password against the values.
password in common.values()would compare it withTrue, and nothing would match.
Testing
"qwerty"givesCommon: TrueandWeak: reject."password"givesCommon: TrueandWeak: reject."Tr0ub4dor"givesCommon: FalseandStrong: accept."QWERTY"givesCommon: FalseandStrong: accept.
