Skip to main content

Ex 16: Student Score Report

Problem

Build a report generator that pairs student names with their corresponding marks. We have two separate lists: one for names and one for scores.

Rules

  • Define generate_score_report(names: list[str], scores: list[int]) -> list[str].
  • Use the zip() function to iterate over both lists in parallel.
  • Return a list where each item is formatted as "Name scored X marks".

Boilerplate

Copy below code and paste to our IDE for head start.

# Write our code inside this function
def generate_score_report(names: list[str], scores: list[int]) -> list[str]:
pass # remove this

# Call with test data
names = ["Hitesh", "Sonali", "Ali"]
scores = [50, 70, 100]
print(generate_score_report(names, scores))

Expected Output

['Hitesh scored 50 marks', 'Sonali scored 70 marks', 'Ali scored 100 marks']

Solution

Only view the solution after trying our best.

Show solution
def generate_score_report(names: list[str], scores: list[int]) -> list[str]:
report: list[str] = []
for name, score in zip(names, scores):
report.append(f"{name} scored {score} marks")
return report

# Test call
names = ["Hitesh", "Sonali", "Ali"]
scores = [50, 70, 100]
print(generate_score_report(names, scores))

Details

zip() is the standard tool for parallel iteration. It's much cleaner than using range(len(names)) and then indexing into both lists manually.