Unit 2 Task 47: Battery drain simulation

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

Unit 2 Task 47 - Battery drain simulation

← Unit 2 tasks · Solution


A phone battery starts at 100%. Each hour, a random integer drain between 5 and 15 percent happens (random.randint(5, 15)). Subtract until the battery is 0 or below.

Print the percentage after each hour (or only final). Print how many hours it lasted.

Skills: while, random, careful clamping or allow negative then report 0.