← Back to Blog

Common FOR Mistakes

The problem...

Your loop runs. But the output is wrong. Or it crashes. Or it does nothing at all.

These are the mistakes almost every beginner makes with for. At least once.

Mistake 1 — Missing the colon

# Wrong — SyntaxError
for letter in "Bull"
    print(letter)

# Right
for letter in "Bull":
    print(letter)

Same rule as if. The for line always ends with :.

Mistake 2 — Wrong indentation

# Wrong — IndentationError
for letter in "Bull":
print(letter)

# Right
for letter in "Bull":
    print(letter)

Everything inside the loop must be indented — 4 spaces. No indent, no loop.

Mistake 3 — Modifying the loop variable and expecting it to change the sequence

word = "Bull"

for letter in word:
    letter = "X"    # changes the variable — not the string
    print(letter)   # X X X X

letter is a copy of the current item. Changing it doesn't change the original string. Strings are immutable — you can't modify them in place.

Mistake 4 — Defining the counter inside the loop

# Wrong — resets on every iteration
for letter in "Bull":
    count = 0
    count += 1
print(count)    # always 1

# Right — define before the loop
count = 0
for letter in "Bull":
    count += 1
print(count)    # 4

A counter defined inside the loop resets every iteration. Always define it before.

Mistake 5 — range(stop) starting at 1

# Wrong — expecting 1 2 3 4 5
for i in range(5):
    print(i)    # 0 1 2 3 4 — not 1 to 5

# Right
for i in range(1, 6):
    print(i)    # 1 2 3 4 5

range(5) starts at 0. Always. Use range(1, 6) if you want 1 to 5.

Mistake 6 — stop is included

# Wrong — expecting 1 2 3 4 5 6
for i in range(1, 6):
    print(i)    # 1 2 3 4 5 — 6 is never printed

# Right
for i in range(1, 7):
    print(i)    # 1 2 3 4 5 6

Stop is never included. range(1, 6) gives you 1 to 5 — not 6.

What's really happening

Most of these mistakes are not about not understanding for.

They're about assumptions. You assume the counter resets. You assume range starts at 1. You assume modifying the variable changes the original.

It doesn't. Be explicit. Always.

The mindset shift

Stop thinking: "This should work."

Start thinking: "Let me check exactly what Python expects."

What you should understand now

  • Every for line ends with :
  • Indentation is syntax — always 4 spaces
  • The loop variable is a copy — modifying it doesn't change the original
  • Define counters before the loop — never inside
  • range(5) starts at 0 — use range(1, 6) for 1 to 5
  • Stop is never included — range(1, 6) gives 1, 2, 3, 4, 5
[ login to bookmark ] // copied! 32 views · 2 min
// resources
Other for_mistakes.py
← prev FOR Mini Project — Form Wannabe next → for loops — The Full Picture
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.