In the last lesson, we learned functions—defining reusable code with def, using parameters (inputs) and return (outputs), writing default parameters and multiple return values, and understanding local scope. Now students try to finish the following practice to review what we learned last unit:
names and scores, write build_map(names, scores) that returns a dictionary mapping name → score. Then write lookup_score(d, name) that safely looks up a score (returns "Not Found" if missing).Output example:
Dict: {'Alice': 90, 'Bob': 85, 'Cathy': 92}
Bob: 85
David: Not Found
Answer example:
def build_map(names, scores):
result = {}
for n, s in zip(names, scores):
result[n] = s
return result
def lookup_score(d, name):
return d.get(name, "Not Found")
names = ["Alice", "Bob", "Cathy"]
scores = [90, 85, 92]
roster = build_map(names, scores)
print("Dict:", roster)
print("Bob:", lookup_score(roster, "Bob"))
print("David:", lookup_score(roster, "David"))
This revisits parameters and returns while motivating a structure that organizes data by name → info, exactly what dictionaries are designed for. When we need to find values by names (keys), lists are clumsy. Dictionaries make lookups, updates, and summaries straightforward. Next, we’ll learn how to create, access, update, delete, and iterate over dictionaries—and apply them in a mini project.