Writing Clean Python Code, for Kids

A name never changes what code does — only whether a human reading it later can tell what it's for.

  • 12 exercises
  • 4 lessons
  • Ages 10+
  • PRO

This topic is part of PRO — a one-time $49.99 purchase, no subscription. Topics 1–3 are free, start to finish, and the first level needs no account.

For parents & teachers

What this topic covers

This topic opens with a demonstration rather than an assertion: the exact same calculation, once written with meaningless single-letter variables and once with real names, produces the identical output both times. Python genuinely does not care what anything is called — the entire value of a good name is for the human reading the code afterward, and stating that plainly, rather than treating good naming as an unexplained style preference, is the point.

Magic numbers — bare literals with no stated meaning nearby — get the same treatment: not a warning to take on faith, but a demonstrated bug. The same threshold, typed as a bare number in two different places, quietly drifts out of sync when only one occurrence gets updated. A named constant makes that specific failure structurally impossible, since both checks then read the same name rather than two literals that merely happen to currently match.

The topic closes on two levels of duplication, taught as genuinely different skills: spotting identical repeated lines, and the harder task of recognising two functions that look different — different names, a different hardcoded number — but share the exact same underlying shape. Extracting that shared shape into one parameterised function is where the topic ends, with a combined refactor level whose only real test is behaviour preservation: the code must do exactly what it did before, and do it more clearly.

Built around a real misconception

The mistake this topic is built to fix

Common mistake

A child treats naming, named constants, and avoiding duplication as arbitrary style preferences with no real consequence if ignored.

Why the topic is built around it

Each idea in this topic is demonstrated as a concrete, reproducible failure rather than argued as good taste: identical output regardless of naming, a real inconsistency bug from a duplicated magic number, and a structural-duplication pair that a naive text search would miss entirely. The goal is a learner who can point to a specific reason each practice matters, not one reciting a rule.

codequality.pyPython 3
x = 100y = 30z = x - yprint(z)# vs.health = 100damage = 30remaining_health = health - damageprint(remaining_health)
Output70 70

Explain · Ask · Guided · Practice

The lessons, in order

Each lesson explains one idea in short steps, checks it landed with a question, walks through a guided first try, then leaves the child to practise on their own.

  1. A name never changes what code DOES — only whether a human can tell what it's FOR.

    • Explain
    • Ask
    • Guided 1 level
    • Practice 2 levels
  2. A bare number with no stated meaning is a bug waiting to happen the moment it needs to change.

    • Explain
    • Ask
    • Guided 1 level
    • Practice 2 levels
  3. Some duplication is the same lines repeated. Some is harder to see — the same SHAPE, wearing different names.

    • Explain
    • Ask
    • Guided 1 level
    • Practice 2 levels
  4. Bad names, a magic number, all in one function — refactor it without changing what it does.

    • Explain
    • Ask
    • Guided 1 level
    • Challenge 2 levels

Lessons unlock in order within a topic, so nothing arrives before the idea it depends on. This topic is part of PRO — $49.99 unlocks it along with everything else.

Try it now

The very first level needs no account at all — see whether it clicks before signing up for anything.

Play the first level free