Start learning free

LESSON 168 OF 410 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 - Common Password Dictionary in Python

Practice

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

    Password Strength V5 in Python

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free