Python · Functions
Challenge 2
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Battle System - Codémon Creation
Start building a battle system! Create a function to make Codémons: -
create_codemon(name, hp, attack, defense)
that returns a dictionary with these stats - Test by creating
Aquachu
(100 HP, 60 attack, 30 defense)
and
Pyrochu
(80 HP, 60 attack, 20 defense)
, then display their stats.
Starter code
# Create create_codemon function # Test it
Exercise 2 — Battle System - Damage Calculation
Add damage calculation to your battle system: - Keep the
create_codemon
function in your starting code - Create
calculate_damage(attacker, defender)
that returns: attacker's attack - defender's defense (minimum 5 damage) - Test by creating two Codémons and calculating damage in both directions.
Starter code
def create_codemon(name, hp, attack, defense): return { "name": name, "hp": hp, "attack": attack, "defense": defense } # Create calculate_damage function # Test it
Exercise 3 — Battle System - Complete Battle
Complete your battle system with damage application and status check: - Keep previous functions in your starting code - Create
apply_damage(codemon, damage)
that reduces HP and returns the updated Codémon - Create
is_alive(codemon)
that returns
True
if HP > 0,
False
otherwise Simulate a complete battle turn: Pyrochu attacks Aquachu, apply damage, and check if Aquachu is still alive.
Starter code
def create_codemon(name, hp, attack, defense): return { "name": name, "hp": hp, "attack": attack, "defense": defense } def calculate_damage(attacker, defender): damage = attacker["attack"] - defender["defense"] if damage < 5: damage = 5 return damage # Create apply_damage function # Create is_alive function # Test it
Back to the course