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

  1. Lesson 16

    Split an input line into a cow name and an amount.

  2. Lesson 17

    Create a list, access a record by index and append new records.

  3. Lesson 18

    Traverse a list to compute a total, find a maximum and count matching items.

  4. Lesson 19

    Use a dictionary to count each name, handling a first occurrence with get.

  5. 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.