Lessons 16–20 · About 60 min per lesson
Strings, lists and dictionaries
Organise records to look up, count and summarise them by name.
Lesson breakdown
- Lesson 16
Split an input line into a cow name and an amount.
- Lesson 17
Create a list, access a record by index and append new records.
- Lesson 18
Traverse a list to compute a total, find a maximum and count matching items.
- Lesson 19
Use a dictionary to count each name, handling a first occurrence with get.
- Lesson 20
Sum amounts by cow name and check a lookup for a missing name.
Worked example
Given Mia 2, Bo 5 and Mia 3, how much feed did each cow receive?
How to solve it
Use each cow name as a key. Update Mia from 2 to 5, leave Bo at 5, and the farm total is 10 kg.
Assignment
Read delivery records and a name, then print the total for that name and the total for all cows.
Check your result
The example prints 5 and 10 for Mia, and 0 and 10 for the missing name Leo.