Unit 2 Task 47 - Solution

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

Solution - Task 47: Battery drain simulation

← Back to task


One possible program:

import random

battery = 100
hours = 0

while battery > 0:
    drain = random.randint(5, 15)
    battery -= drain
    hours += 1
    print("After hour", hours, "battery ~", max(battery, 0))

print("Battery dead after", hours, "hours.")