Lesson 13 of 35Python
Python · Lesson 13 · 45 minutes
Boss level: the school report program
What you'll learn
- Combine everything from the beginner stage in one project
- Analyse data with dictionaries, loops and functions
- Present a result clearly
Welcome to the boss!
At the end of every stage there’s a boss level: a project that combines everything you learned in that stage. No new material – but this time you decide yourself which tools to combine and how.
Your mission: a program that works out the average, the best subject and the shaky ones from a school report.
Your tools
You only need things you already know. A little reminder:
marks = {"Maths": 2, "German": 3, "English": 1}
# All values, all keys, all pairs
print(list(marks.values()))
print(list(marks.keys()))
for subject, mark in marks.items():
print(subject, "→", mark)
# Calculating with lists
print(sum(marks.values()), len(marks))
print(round(7 / 3, 1))
print(sorted(["Physics", "Chemistry", "Maths"]))Finding the best
One idea for “best subject”: remember the best one so far and compare every further one with it – like in the bug detective lesson, but the right way round this time:
points = {"Lea": 12, "Tim": 17, "Ida": 9}
best_name = None
for name, value in points.items():
if best_name is None or value > points[best_name]:
best_name = name
print("Winner:", best_name)Pro shortcut:
max(points, key=points.get)does the same in one line. For the report you need its counterpartmin.
Plan before you type
- First write down in plain words what the function should do step by step.
- Then build one part after another and check it with
print(). - Only when all three parts are right:
returnthe finished dictionary.
Once you’ve beaten the boss, you’ve mastered the beginner stage. 🏆
Your task
Your turn
Write the function report(marks). It gets a dictionary subject → mark (as in German schools, 1 is best and 6 is worst) and returns a dictionary with: "average" (rounded to one decimal place with round(..., 1)), "best_subject" (the subject with the smallest mark) and "resit" (an alphabetically sorted list of all subjects with mark 5 or 6).
def report(marks):
# Tip: sum(), len(), min() with key= or a loop, sorted()
pass
marks = {"Maths": 2, "German": 3, "English": 1, "Physics": 5}
result = report(marks)
print(result)- Something to think about
At the end of the function:
return{"average": ...,"best_subject": ...,"resit": ...} - Which command helps?
Total of the marks divided by the number of subjects –
sum(marks.values())/len(marks)– thenround(..., 1).You're looking for the subject with the smallest mark. Loop through
marks.items()and remember the best subject.Collect all subjects with mark 5 or 6 in a list and return it sorted –
sorted(list). - Gap code – fill in the ___
def report(marks): average = ___ best_subject = ___ resit = ___ return ___ marks = {"Maths": 2, "German": 3, "English": 1, "Physics": 5} result = report(marks) print(result)
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
def report(marks):
average = round(sum(marks.values()) / len(marks), 1)
best_subject = min(marks, key=marks.get)
resit = sorted(subject for subject, mark in marks.items() if mark >= 5)
return {"average": average, "best_subject": best_subject, "resit": resit}
marks = {"Maths": 2, "German": 3, "English": 1, "Physics": 5}
result = report(marks)
print(result)