Average of grades
Reads grades into a list and computes the average. Teaches lists, do-while and looping over a list.
Open in Tokbit Level: Intermediate. Free in the browser, nothing to install, no account needed.
Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.
Flowchart
Python program
The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.
"""Computes the average grade. Teaches a list and checking input."""
count: int
count = int(input("How many grades do you have?"))
grades: list[int] = []
for i in range(count):
z: int
# Asks again until the grade is 1 to 5.
while True:
z = int(input("Grade (1 to 5):"))
if not (z < 1 or z > 5):
break
grades.append(z)
# With no grades we would divide by zero.
if len(grades) > 0:
total: int = 0
for i in range(len(grades)):
# In Python the same can be written as sum(grades).
total = total + grades[i]
print("The average is", round(total / len(grades), 2))
else:
print("No grades.")
Follow-up tasks
Tasks in which you change the example (with a model solution):