Python by Projects › Word Wizard

Exercises: Word Wizard

Your WORD WIZARD needs a bigger phrasebook and a sharper spell-checker. Teach it to translate safely, tidy messy searches, and count the words in a growing log. Write your work in each blank code cell.

Exercise 1: Build a Phrasebook

Build a dictionary named translations with these three English-to-Spanish pairs: "sun" maps to "sol", "moon" maps to "luna", and "star" maps to "estrella". Use one dictionary literal, then print the dictionary.

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 2: Safe Lookup

A full Word Wizard game might ask the player for a word with input("Word? "), but do not call input() in your code cell. Instead, use translations = {"cat": "gato", "dog": "perro"} and the fixed sample variable word = "fish". Look up word with .get(word, "???"), store the result, and print it.

Real program

After the fixed version works, read word with input(). Keep the phrasebook, safe lookup, and output unchanged.

How this is checked: Checked by comparing what your program prints with the expected output.

Answer format: one line

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 3: Is It in the Book?

Start with translations = {"cat": "gato", "dog": "perro"} and word = "cat". Store the result of word in translations in a variable named known, then print known. After that, use known as the condition of an if/else and print a friendly f-string on each path. Repeat the store-then-print check with word = "fish". Do not print either membership expression inline.

Real program

After the fixed version works, make two separate input() reads: one known word and one missing word. Keep the two sequential checks and outputs; do not turn them into a loop.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 4: Add a Word

Start with translations = {"cat": "gato"}. Add the key "bird" with the value "pajaro" by assigning through square brackets. Print the grown phrasebook, then use a friendly f-string to print the translation stored at "bird". Finally, set new_words = 1 and print "New words: " + str(new_words) using exactly + and str() (not an f-string or .format()).

Real program

After the fixed version works, read the new word and meaning with separate input() calls. Keep new_words = 1 fixed and keep every output.

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 5: Grow the Log

Start with words = ["cat", "dog", "cat"] and new_word = "cat". Append new_word to words. Then reset with counts = {} and use a single loop with if/else to count the entire grown list from scratch. Print the new dictionary. Store counts["cat"] > counts["dog"] in cats_lead, then print that boolean variable. Resetting keeps the old words from being counted twice.

Real program

After the fixed version works, read the three starting words with fixed-count input() calls and read new_word separately. Keep the append, recount, and outputs.

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 6: Tidy, Then Translate

Use translations = {"cat": "gato", "dog": "perro"} and raw_word = " CAT ". Build a clean word with .strip().lower(), use the clean word to look up its translation, and print the clean word and "gato" together in a friendly f-string.

Real program

After the fixed version works, read raw_word with input(). Keep the cleaning, lookup, and output unchanged.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 7: Print Every Pair

Start with translations = {"hello": "hola", "cat": "gato", "dog": "perro"}. Use one loop over translations.items() to receive each word and translation together. Print every pair in a friendly f-string.

Real program

After the fixed version works, build the three-pair phrasebook with fixed-count key and value input() calls. Keep the .items() walk and output unchanged.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 8: Fix the KeyError

A Word Wizard tried a missing key and Python actually raised this error:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyError: 'fish'

Before moving on, explain aloud or in your notebook notes what KeyError: 'fish' tells you: name the missing key and why the lookup failed.

After reading and explaining the traceback, write translations = {"cat": "gato"} in your code cell. Use .get("fish", "???") instead of the crashing square-bracket lookup, store the safe result, and print it.

More Practice — Homework: Exercises 9–11

Use these after the three in-class lessons to make the same Word Wizard ideas automatic. The algorithm-pattern exercises and extra loop drills follow in the Algorithm Extension.

How this is checked: Checked by comparing what your program prints with the expected output.

Answer format: one line

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 9: Scoreboard Lines

Start with counts = {"owl": 3, "fox": 1, "cat": 2} and scoreboard = []. Loop over counts.items(). For each pair, build a variable named line using exactly word + " => " + str(count), print line, and append it to scoreboard. Do not use an f-string or .format(). Also store count >= 3 in is_champion and print that boolean variable on each turn. Print scoreboard after the loop.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 10: Visit Labels

Start with visits = {"cave": 2, "tower": 4, "lake": 1} and labels = []. Loop over visits.items(). For each pair, build label with the place, then +, then the literal string " visits: ", then +, then str(visit_count). Print each label and append it to labels. Do not use an f-string or .format(). Print labels after the loop.

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 11: Walk the Keys

Start with translations = {"sun": "sol", "moon": "luna", "star": "estrella"}, visited_words = [], and visited_count = 0. Write for word in translations: to loop over the dictionary's keys directly — do not use .items(). Inside the loop, print the key and translations[word], append the key to visited_words, and add 1 to visited_count. Print the finished list and count after the loop.

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Algorithm Extension

Extra loop practice — optional enrichment: these drills reuse the same loop shapes on small fixed data, so try as many as you like.

Exercise 12: Count the Words

Use the given log words = ["owl", "fox", "owl", "cat", "owl"] and start with counts = {}. Use a single loop over words. If a word is already in counts, add 1 to its count; otherwise, give its first sighting the count 1. Then use a second loop over counts.items() to print each word and count in a friendly f-string. Finally, compare the counts for "owl" and "fox" with > and print the resulting boolean.

Pattern Spotlight retrieval: Which pattern is this, and where is each count-so-far stored? Variation axis — tally-by-key: a dictionary keeps one counter per word, the count-per-group variation.

Real program

After the fixed version works, read all five words with fixed-count input() calls. Keep both loops and every output.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 13: Most Common Word

Start with counts = {"owl": 3, "fox": 1, "cat": 1}, best_word = "", and best_count = 0. Use a single loop over counts.items(). On each turn, store count > best_count in is_new_best and print that boolean variable. If is_new_best is true, save both the new count and its word. Print the most common word and its count after the loop.

Pattern Spotlight retrieval: Which pattern keeps both best_word and best_count? Variation axis — data shape: dictionary word/count pairs instead of parallel name and score lists.

Real program

After the fixed version works, build the three-entry tally with fixed-count word and integer input() calls. Keep the find-extreme loop and outputs unchanged.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 14: Translate a List

Define translate(word, dictionary) so it returns dictionary.get(word, "???"). Use translations = {"hello": "hola", "cat": "gato"}, words = ["cat", "hello", "fish"], and an empty list named translated_words. Loop over words, call translate, and append each result to translated_words. Print the finished list.

Pattern Spotlight retrieval: Which pattern is being reused? Variation axis — dictionary values: each source word produces one translated value.

Real program

After the fixed version works, read all three words with fixed-count input() calls. Keep the helper, transform loop, and output unchanged.

How this is checked: Checked by tests that run your function translate.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 15: Reverse Lookup

The wizard knows a Spanish word and wants to find its English key. Use translations = {"cat": "gato", "dog": "perro", "bird": "pajaro"}, target = "perro", and english_word = "not found". Loop over the dictionary keys one by one. Compare translations[word] for each current key with target; when they match, save that key in english_word and use break to stop checking the rest. Print english_word after the loop. Do not jump straight to translations["dog"] or .get("dog"); the loop must discover which key matches.

A dictionary finds by name instantly; by value, we're back to checking one by one. Pattern Spotlight retrieval: Which pattern compares in order and stops when it finds a match? Variation axis — data: dictionary values instead of characters or numbers.

Real program

After the fixed version works, read target with input(). Keep the fixed phrasebook, linear search, early stop, and output unchanged.

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 16: Keep only the long words

Start with words = ["cat", "dragon", "owl", "wizard", "sun"], minimum_length = 5, and a NEW empty list named long_words. Loop over words. Inside the loop, use len(word) >= minimum_length as the condition and append only passing words to long_words. Print the new list after the loop. Expected result: ['dragon', 'wizard']. Do not use a list comprehension.

Pattern Spotlight retrieval: Which pattern builds a new list containing only items that pass a test? Variation axis — words and length: filter strings by a minimum length instead of scores by a threshold.

Real program

After the fixed version works, read all five words with fixed-count input() calls. Keep minimum_length = 5, the filter loop, and output unchanged.

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 17: Total of the Tally

Use counts = {"owl": 3, "fox": 1, "cat": 2} and add up all the values with an explicit accumulator loop, not sum(), then print the total. Worked example: total is 6.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 18: Rarest Word

Use counts = {"owl": 3, "fox": 1, "cat": 2} and scan its (key, value) pairs to keep the word with the smallest count. Seed rarest_word and rarest_count from the first pair encountered, not from 0, and do not use min(). Worked example: the rarest word is "fox" with count 1. (Hint: use a boolean like first_pair = True and flip it after the first turn to seed the smallest.)

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 19: Known and Unknown

Use words = ["cat", "zzz", "dog"] and translations = {"cat": "gato", "dog": "perro"}. Loop over words and use two counters to count how many words are in translations (known) and how many are not (unknown). Worked example: 2 known, 1 unknown.

Real program

After the fixed version works, read all three words with fixed-count input() calls. Keep the phrasebook, counters, and output unchanged.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 20: Word Lengths That Fit

Use words = ["owl", "dragon", "cat", "wizard", "sun"] (lengths 3, 6, 3, 6, 3) and budget = 12, and check before adding. For each word, only add its length to a running total if doing so would keep the running total <= 12; otherwise stop entirely, then report how many words fit and the final total length. Worked example: exactly 3 words fit ("owl", "dragon", "cat") for a total of exactly 12 — this exact-hit at 12 is why the boundary rule uses <=, not <.

Real program

After the fixed version works, read all five words with fixed-count input() calls. Keep budget = 12, the check-before-adding loop, and output unchanged.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 21: Word Lengths Until It Tips

Same words, same budget, different rule: use words = ["owl", "dragon", "cat", "wizard", "sun"] and budget = 12, but add each word's length to a running total before checking the budget. Stop as soon as the running total exceeds 12, then report how many words were added, the tipping total, and the tipping word. Worked example: 4 words are added, tipping total is 18, tipping word is "wizard".

Real program

After the fixed version works, read all five words with fixed-count input() calls. Keep budget = 12, the add-then-check loop, and output unchanged.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Challenge 1: Merge Two Phrasebooks

  • Challenge

Start with translations = {"cat": "gato", "dog": "perro"} and new_translations = {"bird": "pajaro", "fish": "pez"}. Use a single loop over new_translations.items(). Copy each pair into translations with translations[word] = translation. Print the grown phrasebook after the loop.

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Challenge 2: Flip the Phrasebook

  • Challenge

Start with translations = {"cat": "gato", "dog": "perro", "bird": "pajaro"} and an empty dictionary named spanish_to_english. Loop over translations.items(). Add each Spanish translation as a key in spanish_to_english, with its English word as the value. Print the flipped phrasebook after the loop.

How this is checked: Checked by tests that run your code.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Back to Word Wizard