wk02.p17Practice exerciseWeek 02 · Organizing dataOpen · due this week

Tally the responses

Count how many times each answer appears using a dictionary — the classic counting pattern, where .get() with a default does the heavy lifting.

What you'll build

Count how many times each answer appears using a dictionary — the classic counting pattern, where .get() with a default does the heavy lifting.

Requirements

The must-do parts. If any are missing, we'll ask you to take another pass.

  1. Start from a list of answers that has repeats — for example, the country each applicant is targeting.
  2. Build a counts dictionary where each key is an answer and each value is how many times it appeared. Use counts.get(answer, 0) + 1 so a brand-new answer starts at 1.
  3. Loop with .items() and print each answer next to its count.
  4. Print the total number of responses using len() of the original list.
Bonus, if you're feeling brave
  • Print which answer was the most common.
  • Ask the user to add one more response, then re-run the tally so the counts update.

Examples

What your program should look like when it runs. Lines starting with $ are typed by you; the rest is your program.

Example cell runs
UK: 3
Germany: 2
USA: 1
6 responses in total.

Where to start

Copy this scaffold into a new file. You don't have to use it — it's just a friendly nudge.

# Tally the responses

answers = ["UK", "Germany", "UK", "USA", "Germany", "UK"]
counts = {}

# TODO: loop over answers and count each one with counts.get(answer, 0) + 1

# TODO: print each answer and its count with a .items() loop

# TODO: print the total number of responses with len(answers)

How we'll grade it

Four checks, four points. Three or above is passing — we'll ask you to revise anything we can't tick.

CheckWhat we look forPt
It runsPrints a count for every distinct answer without crashing.1
Counts with a dictionaryTallies are kept in a dictionary keyed by answer — not separate counters per option.1
Uses .get with a defaultNew answers start at 1 via `counts.get(answer, 0) + 1` (or an equivalent first-seen check), so nothing crashes on the first sighting.1
Totals are rightEach count matches the list, and the counts add up to `len()` of the original list.1
Ready?
Hand it in
You can submit a draft and revise later if you're not done.
Begin submission →