← Intro to programming with Python

Mini project: grade averager

Build small functions and combine them into a report.

Start this lesson

Big programs are built from small functions that each do one job well. In this project, one function averages a list of scores, another turns an average into a letter grade, and a final part uses both to print a report for each student. Because each function is tested separately, it is easy to trust the whole.

def average(scores):
    return sum(scores) / len(scores)

def letter(avg):
    if avg >= 90:
        return "A"
    elif avg >= 80:
        return "B"
    return "C or below"

print(letter(average([85, 92, 78])))   # B

The output of average goes straight into letter. That only works because both functions return their answers.

Common mistake: dividing by len(scores) when the list is empty gives a ZeroDivisionError. A careful function checks for an empty list first.