What you'll build
Count how many times each word appears in a sentence. The classic exercise that puts dictionaries to honest work.
Requirements
The must-do parts. If any are missing, we'll ask you to take another pass.
- Start with this sentence:
"the rain in spain falls mainly on the plain and the plain is wide". - Split it into words with
.split()and build a dictionary mapping each word to how many times it appears. - Print the dictionary in a readable way — one
word: countpair per line. - Print the top three most common words by count.
Bonus, if you're feeling brave
- Take the sentence from
input()instead of hard-coding it. - Lowercase everything before counting so
"The"and"the"are treated the same.
Examples
What your program should look like when it runs. Lines starting with $ are typed by you; the rest is your program.
Example output (partial)
the: 3
rain: 1
in: 1
spain: 1
...
Top 3:
the (3)
plain (2)
rain (1)Where to start
Copy this scaffold into a new file. You don't have to use it — it's just a friendly nudge.
word_counter.py
sentence = "the rain in spain falls mainly on the plain and the plain is wide"
words = sentence.split()
counts: dict[str, int] = {}
# TODO: loop over words and fill in counts
# TODO: print each word and its count
# TODO: print the top three words by countHow 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 | Top to bottom, no errors. | 1 |
| Counts correctly | `the` shows up 3 times, `plain` shows up 2. | 1 |
| Readable output | Each `word: count` pair is on its own line. | 1 |
| Top three | The top three words by count are printed clearly at the end. | 1 |