The dragon explains
Spotting duplication
The dragon: Three print lines, only the name different each time — that's the easy kind of duplication to spot.
The dragon: A function with a parameter fixes it directly — write the pattern once, call it as many times as needed.
example.pyPython 3def greet(name): print('Hello, ' + name + '!')greet('Ember')greet('Blaze')
OutputHello, Ember! Hello, Blaze!The dragon: Now a harder case. These two functions LOOK different — different names, different number — but watch their SHAPE.
The dragon: take_damage_ember and take_damage_blaze do the exact same thing: subtract a number from hp. Only the number differs.
example.pyPython 3def take_damage_ember(hp): return hp - 10def take_damage_blaze(hp): return hp - 15
The dragon: No two LINES here are identical text — so pattern-matching for exact repeats would miss this entirely. But the SHAPE repeats.
The dragon: The fix: one function, with the varying number as a parameter instead of hardcoded.
example.pyPython 3def take_damage(hp, amount): return hp - amountprint(take_damage(40, 10))print(take_damage(40, 15))
Output30 25The dragon: The skill here is recognising STRUCTURE, not just text — two things can be 'the same' even when nothing about them is written identically.
Quick check
Check your understanding
Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.
Question 1 of 2
def take_damage_ember(hp): return hp - 10, and def take_damage_blaze(hp): return hp - 15. Are these two functions duplicated code?
question.pyPython 3def take_damage_ember(hp): return hp - 10def take_damage_blaze(hp): return hp - 15
- Yes — they're the same SHAPE, just with a different number hardcoded in eachCorrect
- No — none of the actual lines are identical, so there's no duplication here
Why: Yes, this is duplication — just the harder-to-spot kind. Both functions have the identical SHAPE (subtract something from hp), differing only in one hardcoded number. That number belongs as a parameter, collapsing both functions into one: take_damage(hp, amount).
Question 2 of 2
take_damage(hp, amount): return hp - amount, replacing the two ember/blaze functions. What's the real benefit of the ONE combined function?
- A future change to how damage is calculated only needs to happen in ONE placeCorrect
- It runs faster, since there's less code overall
Why: The real benefit is maintenance, not speed. If take_damage's formula ever needs to change — say, factoring in armor — a single combined function means changing it in exactly one place. Two separate near-duplicate functions mean remembering to update BOTH, which is exactly the kind of drift magic numbers cause too.
In the game
What you'll practice
Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.
- 1
GuidedYour first try
This code greets three dragons with three nearly-identical print lines — only the name changes each time. Extract the repeated pattern into a function, greet(name), and call it three times instead.
- +2
PracticeTwo more, on your own
This lesson is part of PRO.
PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.
Unlock this lesson with PROStuck three times? The game offers the answer — unasked.