Level 0 / Project 06 - Word Counter Basic¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | Diagram | Browser |
Estimated time: 20 minutes
Focus¶
- string splitting and counting
Why this project exists¶
Count words, lines, and characters in a text file, then find the most frequent words. You will practise string splitting, dictionary-based counting, and building summary statistics.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
Type or paste text, then press Enter on a blank line to see the analysis.
Expected terminal output¶
=== Word Counter ===
Type or paste text below. Enter a blank line when done.
Python is a great language.
Python is easy to learn.
Python is powerful and fun.
=== Word Count Summary ===
Lines: 3
Words: 16
Characters: 80
Unique: 11
Top words:
python: 3
is: 3
a: 1
great: 1
language: 1
5 passed
Expected artifacts¶
- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add an "average word length" metric to
analyse_text(). - Ask the user "How many top words to show? " and use that number instead of the default 5.
- Re-run script and tests.
Break it (required) — Core¶
- Enter no text (just press Enter immediately) -- does
analyse_text()crash? - Enter only punctuation like
!!! ??? ...-- are those counted as words? - Enter unicode characters like emojis -- does
count_characters()count them correctly?
Fix it (required) — Core¶
- Add a guard for empty text that returns zero counts without dividing.
- Ensure
word_frequencies()strips punctuation before counting so"hello!"and"hello"are the same word. - Add a test for the empty-text edge case.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- Why does
word_frequencies()use.lower()before counting? - What does
dict.get(key, 0) + 1do and why is it better than checkingif key in dict? - Why separate
count_words(),count_lines(), andcount_characters()into their own functions? - Where would word counting appear in real software (search engines, document analysis, readability scores)?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
- Collections Explained
- Files and Paths
- How Loops Work
- The Terminal Deeper
- Quiz: Collections Explained
Stuck? Ask AI¶
If you are stuck after trying for 20 minutes, use one of these prompts:
- "I am working on Word Counter Basic. I got this error: [paste error]. Can you explain what this error means without giving me the fix?"
- "I am trying to count word frequencies using a dictionary. Can you show me how
dict.get(key, 0)works with a simple example that is not about words?" - "Can you explain the difference between
.split()and.split(' ')with examples?"
| ← Prev | Home | Next → |
|---|---|---|