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.
- Start from a list of answers that has repeats — for example, the country each applicant is targeting.
- Build a
countsdictionary where each key is an answer and each value is how many times it appeared. Usecounts.get(answer, 0) + 1so a brand-new answer starts at 1. - Loop with
.items()and print each answer next to its count. - 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.
| Check | What we look for | Pt |
|---|---|---|
| It runs | Prints a count for every distinct answer without crashing. | 1 |
| Counts with a dictionary | Tallies are kept in a dictionary keyed by answer — not separate counters per option. | 1 |
| Uses .get with a default | New 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 right | Each count matches the list, and the counts add up to `len()` of the original list. | 1 |