Skip to content
Codewilds

Lesson 8 of 34 · Fundamentals

Boss: Python Password Strength Checker

Boss battlehard100 XP1 min readFree lesson

Mossjaw the Gate Gargoyle blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.

The road out of the Fundamentals ends at a mossy stone gate guarded by Mossjaw the Gate Gargoyle, who only lets travellers through if they know a strong passphrase. Trouble is, gargoyles can't read — so build Mossjaw a rune-checker that judges passphrases, and the gate is yours.

Battle plan

This fight uses everything from Chapter 1: string length with len(), a for loop over the characters of a string, boolean flags, if/elif/else, and an f-string for the final answer.

These string methods test a single character (they work on longer strings too):

"A".isupper()   # True  -> uppercase letter
"a".islower()   # True  -> lowercase letter
"7".isdigit()   # True  -> digit
"!".isalnum()   # False -> not a letter or digit, so it's a symbol
" ".isalnum()   # False -> spaces count as symbols too

Assignment

Write a function rate_passphrase(passphrase) that returns a string describing how strong a passphrase is.

  1. If the passphrase has fewer than 8 characters, return "rejected" and stop.
  2. Otherwise, award one point for each rule the passphrase satisfies:
    • it contains at least one uppercase letter
    • it contains at least one lowercase letter
    • it contains at least one digit
    • it contains at least one symbol — any character that is not a letter or digit (spaces count)
    • it is at least 12 characters long
  3. Pick a label from the total: 0–2 points → "weak", 3–4 → "medium", 5 → "strong".
  4. Return the label and the points in the format "<label>: <points>/5".
rate_passphrase("hiss")               # "rejected"
rate_passphrase("password")           # "weak: 1/5"    lowercase only
rate_passphrase("Password1")          # "medium: 3/5"  upper, lower, digit
rate_passphrase("Tr0ub4dor&3")        # "medium: 4/5"  all four kinds, but only 11 characters
rate_passphrase("Serpent-Hollow-42")  # "strong: 5/5"

Each passing test chips a piece off Mossjaw. Make all nine pass to open the gate.

Keep learning

Need a quick reminder? The Python cheat sheet has every topic of Learn Python on one page.

Lesson last updated on .

Write code ↓

Mossjaw the Gate Gargoyle

HP ???

Python+100 XP

Press Run to execute your code, or Submit to check it against the tests.