Start learning free

LESSON 196 OF 287 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 in Python: A Score from 0 to 100

Practice Read the full explanation

Combine the earlier checks into a single numeric score between 0 and 100.

You start with:

password = "Secret!99"

Build:

Length: 9
Score: 100

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.
  • Start with score = 0.
  • If the length is at least 8, add 25.
  • If the password contains an uppercase letter, add 20.
  • If it contains a lowercase letter, add 20.
  • If it contains a digit, add 20.
  • If it contains a symbol (not a letter and not a digit), add 15.
  • Add the line "Length: " followed by the length, then add the line "Score: " followed by the score.

Try other passwords: with abc123 the output is

Length: 6
Score: 40

Same character-class detection as v3, then five independent if statements that add to score.

password = "Secret!99"

length = len(password)
has_upper = False
has_lower = False
has_digit = False
has_symbol = False
for c in password:
    if c.isupper():
        has_upper = True
    if c.islower():
        has_lower = True
    if c.isdigit():
        has_digit = True
    if not c.isalnum():
        has_symbol = True
score = 0
if length >= 8:
    score += 25
if has_upper:
    score += 20
if has_lower:
    score += 20
if has_digit:
    score += 20
if has_symbol:
    score += 15
lines = []
lines.append("Length: " + str(length))
lines.append("Score: " + str(score))
receipt = "\n".join(lines)

STEP 1 OF 20
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V6 in Python, explained

    Problem overview

    Version 6 rates a password with a number. It checks five facts, and each true fact adds points to a score: a length of at least 8 gives 25, an uppercase letter 20, a lowercase letter 20, a digit 20 and a symbol 15. The five amounts add up to 100. The starting code sets password = "Secret!99", and the receipt in receipt has two lines, the length and the score.

    Length: 9
    Score: 100

    For "abc123" only the lowercase letters and the digits count, so the lines are Length: 6 and Score: 40.

    Approach: find the facts, then add up the points

    The program works in two phases. First, a loop over the characters sets four flags: has_upper, has_lower, has_digit and has_symbol. Then, a series of separate if statements turns the facts into points.

    The scoring statements are independent if statements, not an if/elif chain. Every rule that holds adds its own points, so a password can earn several of them. An elif chain would stop after the first rule that matched.

    Step by step

    1. Create length. len(password), used for the 25-point rule and for the first output line.
    2. Create four flags at False: has_upper, has_lower, has_digit and has_symbol.
    3. Loop over the characters. c.isupper() sets has_upper, c.islower() sets has_lower, c.isdigit() sets has_digit, and not c.isalnum() sets has_symbol. Each test is its own if.
    4. Create score at 0. It is a total that nothing has added to yet.
    5. Add the points. One if for each rule: length >= 8 adds 25, has_upper 20, has_lower 20, has_digit 20 and has_symbol 15, each with score += ....
    6. Create the empty list lines.
    7. Append the length line and the score line, in that order, each with str() on the number.
    8. Create receipt. "\n".join(lines).

    Watching the points add up

    For "abc123", which has no uppercase letter, no symbol and a length of 6, only two rules fire. Printing the score after each rule that adds points gives:

    lower 20
    digit 40

    The final score is 40. The length rule, the uppercase rule and the symbol rule were all skipped, and each skipped rule adds nothing.

    The solution in Python

    password = "Secret!99"
    
    length = len(password)
    has_upper = False
    has_lower = False
    has_digit = False
    has_symbol = False
    for c in password:
        if c.isupper():
            has_upper = True
        if c.islower():
            has_lower = True
        if c.isdigit():
            has_digit = True
        if not c.isalnum():
            has_symbol = True
    score = 0
    if length >= 8:
        score += 25
    if has_upper:
        score += 20
    if has_lower:
        score += 20
    if has_digit:
        score += 20
    if has_symbol:
        score += 15
    lines = []
    lines.append("Length: " + str(length))
    lines.append("Score: " + str(score))
    receipt = "\n".join(lines)
    

    Details worth knowing:

    • The weights are uneven. Length is worth the most, and the symbol is worth the least, 15. The five numbers were picked to sum to exactly 100, so a password that satisfies every rule scores 100.
    • score += 25. It means score = score + 25. It needs score to exist already, which is why score = 0 comes first.
    • A flag works as a condition. if has_upper: is enough, because has_upper is already True or False.
    • The loop and the scoring are separate. The loop runs 9 times for "Secret!99" and only records facts. It doesn't touch score. That keeps each phase easy to read and to test.
    • The scores are only sums of the rules. Not every number from 0 to 100 can occur, because the points come in fixed blocks.

    Complexity

    Time: O(n), where n is the number of characters. The loop makes one pass, and each character gets a fixed number of constant-time tests. The five scoring statements run once each, however long the password is.

    Space: O(1) extra for the flags or counters and the few output lines, whatever the password's length.

    Reading the score

    The score is a sum of independent contributions, so it can be explained rule by rule. A score of 45 means the length rule and one 20-point rule fired, and a score of 55 for a short password means all of uppercase, lowercase and symbol were present. Because each rule adds a fixed amount, one more rule would only mean one more if, with the weights rebalanced to keep the total at 100.

    Edge cases and common mistakes

    • Short passwords can still score. "Abc!" has 4 characters, so it gets no length points, but it earns 20 + 20 + 15 and scores 55.
    • Long, one kind only. "abcdefgh" has 8 characters and lowercase letters, so it scores 25 + 20, which is 45. "ABCDEFGH" also scores 45.
    • All rules at once. "Secret!99" earns 25 + 20 + 20 + 20 + 15, which is 100, the most a password can score.
    • The empty password. Nothing is earned, and the lines are Length: 0 and Score: 0.
    • A space is a symbol. "Secret 99" scores 100, the same as "Secret!99".
    • Using elif for the scoring. Only one rule could add points, and the score would never go past 25.
    • Setting score = 25 instead of adding. Each rule would overwrite the earlier points. The statements must use +=.
    • Putting score = 0 after the rules. It would erase the points just added.

    Testing

    • "Secret!99" gives Length: 9 and Score: 100.
    • "abc123" gives Length: 6 and Score: 40.
    • "abcdefgh" gives Length: 8 and Score: 45.
    • "Abc!" gives Length: 4 and Score: 55.
    • "" gives Length: 0 and Score: 0.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free