CodeAgeBy Sitezack
Final project: your text RPGYour choose-your-own adventure0 XP
Chapter progress0/6

Lesson 3 of 6

The attack function

A function keeps your game tidy: def attack(hp, power) computes the health left after a hit.

max(0, value) stops health from going below zero — a monster at -3 hp doesn't exist!

def attack(hp, power):
    return max(0, hp - power)

🎯 Your mission

Write the function attack(hp, power) that returns hp - power, never going below 0 (use max). Print attack(20, 5).

PRO lesson

This advanced chapter is part of CodeAge PRO. Unlock it to keep your adventure going.

Discover PRO