Start learning free

LESSON 196 OF 673 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in Python: Rejecting Common Passwords

Practice Read the full explanation

Use a dictionary (or set) as a fast lookup table to reject well-known weak passwords.

You start with:

common = {"password": True, "123456": True, "qwerty": True, "letmein": True}
password = "qwerty"

Build:

Password: qwerty
Common: True
Weak: reject

Rules:

  • Collect the output lines in a variable named receipt: one string with the lines joined by "\n" (for example build a list lines and join it). The grader prints receipt for you, so do not call print.
  • Add the line "Password: " followed by the password.
  • Look the password up in the dictionary common and store the result as a boolean (the in operator checks a dictionary's keys).
  • Add the line "Common: " followed by the boolean.
  • If it is common, add the line "Weak: reject"; otherwise add the line "Strong: accept".

Try other passwords: with Tr0ub4dor the output is

Password: Tr0ub4dor
Common: False
Strong: accept

password in common gives the boolean directly.

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)

STEP 1 OF 7
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in Python, explained

    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

    1. Create the empty list lines.
    2. Append the first line. Password: followed by password. The password is already text, so it needs no str().
    3. Create is_common. password in common is True when the password is one of the dictionary's keys.
    4. Append the second line. Common: followed by str(is_common). A boolean has to be turned into text first.
    5. Write the if. When is_common is true, append Weak: reject.
    6. Write the else. Otherwise append Strong: accept.
    7. 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:

    • in checks keys. For a dictionary, x in common looks 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, so if is_common == True adds 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 else is skipped for the starting password. "qwerty" is a key, so only the if branch runs.

    Edge cases and common mistakes

    • Another common password. "password" gives Common: True and Weak: reject.
    • A different case. "QWERTY" gives Common: False and Strong: accept.
    • The empty password. It is not a key, so the lines are Password: (with nothing after it), Common: False and Strong: accept.
    • Writing common[password]. For a password that isn't in the table it raises a KeyError. The in operator never raises.
    • Concatenating the boolean. "Common: " + is_common raises a TypeError. Use str(is_common).
    • Testing the password against the values. password in common.values() would compare it with True, and nothing would match.

    Testing

    • "qwerty" gives Common: True and Weak: reject.
    • "password" gives Common: True and Weak: reject.
    • "Tr0ub4dor" gives Common: False and Strong: accept.
    • "QWERTY" gives Common: False and Strong: accept.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free