Build a word counter

Take your time, follow the code and check your understanding.

Move blocks into orderDictionaries
  1. print(counts["code"])
  2.     counts[word] = counts.get(word, 0) + 1
  3. for word in "code code".split():
  4. counts = {}
Your answer3 min · no timer

Drag blocks, or use the arrow buttons. Keep the displayed indentation. This task uses the taught sequence; other equivalent programs may also be valid.

Show solution and explanation

Create the dictionary before using it. The indented statement belongs to the loop.

counts = {}
for word in "code code".split():
    counts[word] = counts.get(word, 0) + 1
print(counts["code"])