Editor

TDD Challenge: Build a Word Counter

Advanced · Write the code · from Test-Driven Development: Write the Test First, Then the Code

From the lesson: Test-Driven Development: Write the Test First, Then the Code

Imagine you're drawing a picture. Most people sketch the outline first, then fill in the colors. You wouldn't start by painting random patches of blue and hope a sky appears.

Read the lesson

Use TDD to build a word_count function that takes a string and returns a dictionary with each word (lowercased) as a key and its count as the value. Handle these cases: (1) normal sentence, (2) repeated words, (3) mixed case (treat "The" and "the" as the same), (4) empty string returns empty dict. Write a test for each case, run them all, and print PASS: <test_name> for each, then Word counter complete!.

# Use TDD: write tests first, then implement
# Test 1: Normal sentence
# Test 2: Repeated words
# Test 3: Mixed case
# Test 4: Empty string
# Implement word_count
# Run all tests

More TDD exercises

All Python exercises · 50 practice problems · Testing quiz