Reusable Combat Calculations
In World of Software Engineering, players cast spells that deal damage based on a formula.
Instead of rewriting that formula every time, we can create a function.
Instead of rewriting that formula every time, we can create a function.
A function:
• Groups related code together
• Accepts input values (parameters)
• Returns a result
• Can be reused many times
This prevents repetition and keeps our code clean.
• Groups related code together
• Accepts input values (parameters)
• Returns a result
• Can be reused many times
This prevents repetition and keeps our code clean.
Structure of a function:
def function_name(parameter):
# indented code runs here
return result
In this lesson, we have a function that calculates spell damage.
The fireball spell works correctly.
The frost bolt spell is broken.
Fix the bug.
Fix the function call
def calculate_spell_damage(base_power):
multiplier = 1.5
damage = base_power * multiplier
return damage
fire_power = 40
ice_power = 60
# don't touch above this comment
fire_damage = calculate_spell_damage(fire_power)
ice_damage = 0
# don't touch below this comment
print("Fire spell damage:", fire_damage)
print("Ice spell damage:", ice_damage)