← Back to Functions
❤️

Health System

3 of 6

Exercise 2: Health System

Your Task

In games, player health goes up (healing) or down (damage) — but it can never go below 0 or above a maximum.

Write a function update_health(health, change, max_hp) that:

  • Takes the current health, the change (positive = heal, negative = damage), and the max health
  • Returns the new health
  • Health can't go below 0 or above max_hp

Starter Code

def update_health(health, change, max_hp): # Your code here pass # Test your function: print(update_health(50, 20, 100)) # Should print: 70 print(update_health(50, -30, 100)) # Should print: 20 print(update_health(90, 30, 100)) # Should print: 100 (capped!) print(update_health(10, -50, 100)) # Should print: 0 (can't go negative!)

Hints

Hint 1

Start by adding change to health. Then check if the result is too high or too low.

Hint 2
new_health = health + change if new_health > max_hp: new_health = max_hp if new_health < 0: new_health = 0 return new_health
Solution
def update_health(health, change, max_hp): new_health = health + change if new_health > max_hp: new_health = max_hp if new_health < 0: new_health = 0 return new_health
💻

Try it yourself

Code: Health System

Loading Python runtime…
Python
Loading...
Output
Click "Run" to execute your code...
ℹ️ About this Python environment

✅ Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect

✅ Functions, classes, recursion, print()

❌ No file system, subprocess, OS access, or network requests

❌ No pip install (all supported modules are pre-loaded)

⏱️ 5 second execution time limit