Scenario: On sports day, 6 runners finish a race. You will type in each runner’s time in seconds.
The school record is 12.5 seconds.
Your tasks
- Use a
forloop to ask for 6 race times (usefloat). - Keep a running total of all times.
- Count how many times are under 12.5 (faster than the record).
- After the loop, print the total time.
- Print the average time (
total / 6). - Print how many runners beat the school record.
💡 Start
total = 0 and record_beaters = 0 before the loop. Add inside the loop.💡 Try the whole challenge in your editor first. Only reveal the solution when you have had a proper go.
Solution
total = 0
record_beaters = 0
for i in range(1, 7):
time = float(input("Enter time " + str(i) + " in seconds: "))
total = total + time
if time < 12.5:
record_beaters = record_beaters + 1
average = total / 6
print("Total time:", total)
print("Average time:", average)
print("Runners who beat the record:", record_beaters)
Example output:
Enter time 1 in seconds: 11.8
Enter time 2 in seconds: 13.0
Enter time 3 in seconds: 12.2
Enter time 4 in seconds: 14.1
Enter time 5 in seconds: 12.5
Enter time 6 in seconds: 11.9
Total time: 75.5
Average time: 12.583333333333334
Runners who beat the record: 3
💡 12.5 does not beat the record, because the rule is under 12.5. So only 11.8, 12.2 and 11.9 count.