Editor

Fix the Bug: Letter Counter

Beginner · Fix the bug · from Python For Loops: Iterate Over Anything with Examples

From the lesson: Python For Loops: Iterate Over Anything with Examples

Imagine you're a teacher with 30 students. You need to print a report card for each one. Would you write 30 separate print statements? Of course not. You'd tell the computer: "for each student, print their report card." That's exactly what a for loop does.

Read the lesson

This code is supposed to count how many times the letter "l" appears in the word "hello world", but it has bugs. Fix the code so it prints:

The letter "l" appears 3 times

Don't change the word variable or the final print statement - just fix the loop logic.

word = "hello world"
count = 0
for char in word:
if char == "L":
count = 1
print(f'The letter "l" appears {count} times')

More For Loops exercises

All Python exercises · 50 practice problems · Control Flow quiz