3 of 6
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:
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!)
Start by adding change to health. Then check if the result is too high or too low.
new_health = health + change if new_health > max_hp: new_health = max_hp if new_health < 0: new_health = 0 return new_health
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
✅ 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