Combining Everything
Great! Step 4 is here — the final boss of Unit 5.
This is where functions stop being “cute little helpers” and start becoming full game systems.
We’re talking parameters + returns + loops + conditions all teaming up like the Avengers of Python.
Let’s finish this unit with fireworks.
1. Example: A Real Attack Function
Let’s build an attack that:
- takes damage as input
- calculates bonus damage
- returns the final hit
- prints the result
def attack(base_damage):
bonus = 5
total = base_damage + bonus
print("You strike for", total, "damage!")
return total
hit = attack(10)
Now you can do:
hit = attack(10)
Your function just did math, printed action text, AND returned the final number.
Triple threat.
2. Example: A Healing Function With Conditions
def heal(health, amount):
health = health + amount
if health > 100:
health = 100
return health
health = heal(80, 30)
print("New health:", health)
Call it like:
health = heal(80, 30)
print(“New health:”, health)
Your game now heals responsibly.
No overpowered nonsense.
3. Example: Looping Through Enemies With a Function
def introduce(enemy):
print("A wild", enemy, "appears!")
enemies = ["goblin", "orc", "slime"]
for e in enemies:
introduce(e)
Your game now introduces enemies like it’s hosting a fantasy runway show.
4. Example: Full Mini-System — Battle Turn
def take_turn(health, damage):
health = health - damage
print("Enemy hits you for", damage)
print("Your health is now", health)
return health
health = 50
while health > 0:
health = take_turn(health, 10)
This is an actual battle loop.
You just built a tiny Role-Playing Game (RPG) engine.
Practice (Your Turn!)
Mini Challenge
Create a function called use_potion(health, amount) that:
- heals the player
- caps health at 100
- returns the new health
Then call it inside a loop until health reaches 100.
Boom.
You just built a healing system.
End of Unit 5 — You Now Have Superpowers
You can now:
- define functions
- customize them with parameters
- get results with return values
- combine them with loops, lists, and conditions
- build REAL gameplay systems
Your code is officially leveling up.


