🐍 [Day 2] check_password()

April 21, 2026

Morning! πŸ˜ƒ β˜•οΈ

Yesterday you met the whole function.

Today, the first two lines are gone - the length check.

They're the easy ones. But now your brain has to reach for them instead of just reading them, and that little reach is exactly what turns "I've seen it" into "I know it."

Look at the blanks below and try to fill them in your head before you scroll.

In today's email...

THE FUNCTION 🐍

def check_password(password):
    ____________________
        ____________________
    has_number = False
    for char in password:
        if char.isdigit():
            has_number = True
    if has_number:
        return "Strong"
    return "Medium"

TODAY'S LINE πŸ’­

The two blanked lines are the length check:

    if len(password) < 8:
        return "Weak"

len(password) counts how many characters the password has. < 8 asks "is that fewer than eight?" If yes, the function returns "Weak" immediately.

Notice the second line is indented further than the first. In Python, indentation is how the language knows a line belongs inside the if - the return only runs when the condition is true.

And that return does something powerful: it ends the function on the spot. A too-short password never reaches the number check at all - there's no point, it's already weak.

That's the guard clause: the very first box on the map, and the only one with its own exit door.

Flowchart of check_password with the length gate as decision 1, branching True to Weak and False onward to the digit scan

TRACE IT πŸ”

Watch "cat" hit the gate:

That last point is the whole reason the check goes first. Compare it with a password that clears the gate:

Trace table showing cat returns Weak with the digit check never reached, while raspberry and Sunrise7 clear the length gate

"cat" gets a verdict in two lines. "raspberry" and "Sunrise7" have to do the whole walk.

COMMON BUG πŸ›

Get the indentation wrong on these two lines and the function breaks in two very different ways.

Under-indent it and Python refuses to run at all:

    if len(password) < 8:
    return "Weak"

That's an IndentationError: expected an indented block. Python needs something inside the if.

Over-indent it the wrong way and it runs, but lies:

    if len(password) < 8:
        return "Weak"
    return "Weak"

Now every password is "Weak", because that second return sits outside the if and fires for everyone. The first bug shouts. The second one is quiet - and quiet bugs are the expensive ones.

And the classic off-by-one: < 8 lets an 8-character password through. <= 8 would reject it. Yesterday's "Sunrise7" sits exactly on that line.

WHY IT MATTERS 🧠

"Check the deal-breaker first, then leave" is called an early return, and professional code is full of them. Every form validator, every API handler, every file uploader starts the same way: is this obviously wrong? Then stop now.

The alternative - nesting everything else inside an else: - works, but three checks deep it becomes a staircase nobody can read.

WITH ADA β˜•

Type the two missing lines back in, then head to Ada at py-and-jam.com.

Not sure why a line works the way it does? Ask Ada to explain it. Paste the function, point at the length check, and she'll walk you through what len() and < 8 are doing - in plain English, at your pace.

Quick test: run it on "short" and then on "muchlonger". Only the second clears the length gate.

LEVEL UP πŸš€ Add a second guard above the first one, for an empty password:

    if not password:
        return "Weak"

not "" is True, so an empty string gets caught before anything else touches it. Two guards, stacked - exactly how real validators grow.

ANSWER KEY βœ…

Blanked today: lines 2 and 3 - the length check.

    if len(password) < 8:
        return "Weak"

The full function:

def check_password(password):
    if len(password) < 8:
        return "Weak"
    has_number = False
    for char in password:
        if char.isdigit():
            has_number = True
    if has_number:
        return "Strong"
    return "Medium"

See you tomorrow - the loop goes next. 🐍 The Py & Jam Team

There's a better way to learn.

Py & Jam delivers one memorable disappearing function to your inbox daily. It’s a simple, effective way to build fluency without the frustration.