One-sentence summary
In this project we store example training data in a list, then use a loop and a function to calculate the total, average and highest value, and print a short sports summary.
Why it matters
I enjoy sport, and after a training session I often wonder, “How much did I train this week?” Instead of adding the numbers by hand in a notebook, I tried letting the computer do it. This is the first step of data analysis: we take data (the numbers we have), calculate with it and present a short summary.
This lesson brings together three ideas from earlier modules in a single project: a list to store the data, a loop to work through each number in order, and a function to reuse the same calculation again and again. The same method later works for game scores, pocket money or temperature readings.
Note: The numbers here are teaching examples, not a real statistic. You can also type in your own real minutes.
Building the project piece by piece
Instead of writing the whole program at once, we will split it into small parts. The safest way is to run each part and make sure it is correct before moving on to the next one.
1. Putting the data in a list
A list is a structure that holds several values, in order, under a single name. We write them inside square brackets [ ], separated by commas.
# Example training minutes (teaching data)
training_minutes = [30, 45, 60, 40, 50, 55, 35]
print("Number of days:", len(training_minutes))
print("First day:", training_minutes[0])
Every item in the list has a position number (an index), and counting starts at 0. That is why training_minutes[0] gives the first day. len(...) tells us how many items are in the list; here it is 7.
2. Calculating the total and the average
Now we will walk through each number in order and add it to a running total. For this we use a loop.
training_minutes = [30, 45, 60, 40, 50, 55, 35]
total = 0
for minutes in training_minutes:
total = total + minutes
average = total / len(training_minutes)
print("Total minutes:", total)
print("Average minutes:", round(average, 1))
The line for minutes in training_minutes: walks through the list from start to end; minutes takes the next value on each pass. It is very important that the line inside the loop is indented by 4 spaces: Python uses this indentation to know where a block begins and ends. If the indentation is wrong, the program will not run.
For the average we divide the total by the number of days. round(average, 1) rounds the result to one decimal place. Output: total 315, average 45.0.
3. Finding the highest value
To find the longest training session, we keep a “highest so far” variable and compare each number with it.
training_minutes = [30, 45, 60, 40, 50, 55, 35]
highest = training_minutes[0]
for minutes in training_minutes:
if minutes > highest:
highest = minutes
print("Longest session:", highest)
At the start we set highest to the first value in the list. If the loop finds a larger number, it updates highest. Output: 60.
Collecting the code in a function
If we want to reuse the same calculation for different lists again and again, we put the code inside a function. A function is a ready-made package of steps that we give a name and call whenever we need it.
Full code
# Example training data (a teaching example, not a real statistic)
training_minutes = [30, 45, 60, 40, 50, 55, 35]
def stats_summary(data):
total = 0
for minutes in data:
total = total + minutes
average = total / len(data)
highest = data[0]
for minutes in data:
if minutes > highest:
highest = minutes
return total, average, highest
total, average, highest = stats_summary(training_minutes)
print("Number of days:", len(training_minutes))
print("Total minutes:", total)
print("Average minutes:", round(average, 1))
print("Longest session:", highest)
The line def stats_summary(data): defines the function. data is the name the list has inside the function. With return we send back all three results at once and receive them in three variables outside.
Let us test it
When we save the code to a file and run it in the terminal with python3 file.py, we see this:
Number of days: 7
Total minutes: 315
Average minutes: 45.0
Longest session: 60
Let us check the result by hand: 30+45+60+40+50+55+35 = 315, 315 / 7 = 45, and the largest number is 60. The program works correctly.
Mini practice
Try it with your own data:
- Replace the numbers in
training_minuteswith your own values for this week. - Add one more day, for example
70at the end. Confirm that the number of days becomes 8. - Run the program and check by hand whether the new total, average and highest value are correct.
- Enhancement: Also find the shortest session. Write a new section that uses the logic
if minutes < lowestinstead ofif minutes > highest.
A hint for the enhancement:
lowest = training_minutes[0]
for minutes in training_minutes:
if minutes < lowest:
lowest = minutes
print("Shortest session:", lowest)
As a further step, you can count how many days you trained above the average:
above = 0
for minutes in training_minutes:
if minutes > average:
above = above + 1
print("Days above the average:", above)
Project strengthening plan
A working demonstration is not enough for Project: Simple Sports Statistics. A strong project also makes its aim, user, limits, test conditions and failed attempts visible. Use the context of a small game that asks the user to guess a number in a range to produce a short working Python 3 program, sample inputs and expected outputs. Although the lesson aims to “Build a program that computes total, average and maximum from sample training data and prints a summary”, do not present an unmeasured result as a confirmed success.
1. Project summary and scope
Write three sentences: What problem are you solving, who is affected by it, and what will the first version deliberately not do? Stating what is outside the scope does not weaken a project; it makes the project finishable. Describe the connection between Building the project piece by piece and 2. Calculating the total and the average as the main assumption, then name the test that can confirm or reject it.
2. Acceptance criteria
- Does the code run with Python 3 syntax?
- Do variable and function names explain their purpose?
- Were empty, wrong-type and boundary inputs tested?
- Does an error message tell the user what to correct?
- Can the code be reused without unnecessary repetition?
Do not leave an acceptance criterion for “Project: Simple Sports Statistics” as a vague statement such as “it works”. Choose an observable measure such as time, distance, correct trials, screen width or user steps. When direct measurement is difficult, record whether the same behaviour appears in three consecutive trials.
3. Test matrix
| Test | Condition | Expected | Actual result | Next decision |
|---|---|---|---|---|
| Normal | Standard input and complete setup | The core task is completed | Fill in during the test | Keep it or make a small improvement |
| Boundary | Lowest or highest accepted value | The system remains stable | Fill in during the test | Review the threshold or rule |
| Error | Missing, wrong or unexpected input | A safe and clear response | Fill in during the test | Add error handling |
| Repeat | At least three trials under the same condition | Similar results | Fill in during the test | Investigate the source of inconsistency |
4. Version log
For every version of “Project: Simple Sports Statistics”, record the date, the one main decision changed, the reason and the test result. A first version that fails is evidence about which assumption involving Building the project piece by piece or 2. Calculating the total and the average should be reconsidered. Remove personal information and private background details from images.
5. Presentation and self-review
Prepare a two-minute explanation of “Project: Simple Sports Statistics”: the problem, the solution approach, the most important result for a small game that asks the user to guess a number in a range, and the next step. Instead of saying the project is complete, state which part has been verified and which part still needs development.
Common mistakes
Resetting the total inside the loop
If you accidentally put the line total = 0 inside the loop, the total is reset on every pass and the result is wrong.
# Wrong
for minutes in training_minutes:
total = 0
total = total + minutes
Fix: the line total = 0 must be before the loop, with no indentation.
Indentation error
If you do not indent the lines inside the loop, Python raises an IndentationError. Every line of a block must be 4 spaces in.
Starting highest at zero
Writing highest = 0 happens to work in this example, but if all the numbers were negative (for example, temperature readings) it would always give the wrong answer 0. Starting from the first item in the list is safer.
An empty list
If the list is empty, total / len(data) causes a division-by-zero error. In a real project you should first check that the list has at least one item.
Safety note
This lesson is entirely about running your own code, on your own computer. Do not run code you found on the internet if you do not understand what it does. When you type in your data, do not put personal details such as a real address, phone number or school into the code; example numbers are enough.
Review questions
- Which sports data can be compared fairly, and which context must be kept with it?
- Why is an average alone not enough to describe performance?
- How should missing or invalid entries be handled?
- What chart would make change over time easier to see?
- Why must the project avoid medical or professional performance claims?
- What test cases should be run before trusting the calculated summary?
Answers
- Compare measurements collected with the same definition, unit and conditions, while keeping dates, session type and measurement method.
- An average can hide variation, improvement, fatigue, outliers and the number of observations.
- Validate input, mark missing data explicitly and exclude or correct an entry only with a documented rule.
- A time-series line chart with clearly labelled units and dates usually makes direction and variation visible.
- The data is an educational record from limited measurements, not a clinical assessment or expert coaching decision.
- Test an empty dataset, one item, repeated equal values, a clear outlier, decimal inputs and an invalid entry.
Lesson summary
- A list holds many numbers under a single name, in order, and counting starts at 0.
- A loop lets us visit each item in the list in order and build up values such as a total.
- The average is found by dividing the total by the number of items (
len). - We find the highest value by updating a comparison variable inside the loop.
- A function lets us reuse the same calculation on different lists and returns a result with
return.
Check your understanding
- Which day's value does
training_minutes[0]give, and why? - Why must the line
total = 0be written before the loop? - Which two values do we divide to calculate the average?
- In which situation would
highest = 0give a wrong answer instead ofhighest = data[0]? - What is the
defkeyword used for?
Answers
- It gives the first day's value, because counting in a list starts at 0, so the first item's index is 0.
- We reset the total before the loop; if we put it inside, it is reset on every pass and we keep only the last value.
- We divide the total by the number of items in the list (
len(data)). - If all the values are negative (for example, negative temperatures),
0looks like the highest even though it is not in the list, so the result is wrong. defdefines a new function, that is, a ready-made package of steps that we give a name and call whenever we need it.
Source and verification note
For “Project: Simple Sports Statistics”, verification focuses on whether the relationship between Building the project piece by piece and 2. Calculating the total and the average remains consistent across examples. Code examples follow Python 3 syntax. Small differences may appear between environments, so examples should first be tested in a safe online editor or a local development setup.
Next lesson
Web Basics module: We take the programming logic you built with Python into a new area by learning how web pages are put together.