Using incorrect indentation inside loops

Loading

Python relies heavily on indentation to define blocks of code. Unlike other languages that use {} (curly braces), Python uses whitespace (spaces or tabs) to determine where a block of code starts and ends. Incorrect indentation inside loops can cause IndentationErrors, unexpected logic errors, or infinite loops.

In this guide, we’ll go step by step to understand how incorrect indentation affects loops and how to fix it.


1. Understanding Indentation in Python

In Python, code blocks inside loops, functions, and conditionals must be indented consistently.

Correct Example:

for i in range(5):  
print(i) # This is correctly indented
  • The print statement is indented inside the loop, so it runs five times.

Incorrect Example (Missing Indentation):

for i in range(5):  
print(i) # IndentationError: expected an indented block
  • Since print(i) is not indented, Python does not recognize it as part of the loop, causing an IndentationError.

2. Common Indentation Mistakes in Loops

Mistake 1: Missing Indentation in Loops

numbers = [1, 2, 3, 4, 5]

for num in numbers:
print(num) # IndentationError

Fix:

for num in numbers:
print(num) # Correct indentation

Mistake 2: Extra Indentation Outside Loop

    for i in range(3):  #  Unexpected Indentation
print(i)

Error:

IndentationError: unexpected indent

Fix:

for i in range(3):
print(i)

Mistake 3: Inconsistent Indentation (Mixing Tabs and Spaces)

for i in range(3):
print(i) # Using a tab
print("Python") # Using spaces

Error:

TabError: inconsistent use of tabs and spaces in indentation

Fix: Use only spaces (Python recommends 4 spaces per indent).

for i in range(3):
print(i)
print("Python")

Mistake 4: Incorrect Indentation in Nested Loops

for i in range(3):
for j in range(2):
print(i, j) # ❌ IndentationError

Fix:

for i in range(3):
for j in range(2):
print(i, j) # Properly indented

3. How Indentation Affects Loop Logic

Incorrect indentation may not always cause an error, but it can lead to unexpected behavior.

Example: Wrong Indentation Causes Incorrect Loop Execution

for i in range(3):
print("Start of loop")
print(i) # Runs only once instead of inside the loop

Unexpected Output:

Start of loop
Start of loop
Start of loop
2 # Printed once, outside the loop

Fix:

for i in range(3):
print("Start of loop")
print(i) # Properly indented

Correct Output:

Start of loop
0
Start of loop
1
Start of loop
2

4. Indentation in while Loops

Mistake: Incorrect Indentation Causing Infinite Loop

i = 0
while i < 3:
print(i) # IndentationError
i += 1 # This is also not inside the loop

Fix:

i = 0
while i < 3:
print(i)
i += 1 # Properly indented inside the loop

5. Best Practices to Avoid Indentation Errors

  1. Use a Consistent Indentation Style (Python recommends 4 spaces).
  2. Never Mix Tabs and Spaces – Use spaces instead of tabs.
  3. Use an IDE or Code Editor That Highlights Indentation Errors (e.g., VS Code, PyCharm).
  4. Manually Check Indentation for Nested Loops and Conditionals.
  5. Avoid Accidental Extra Indents (Remove unnecessary spaces before for, while, if).

6. Summary of Common Indentation Mistakes

MistakeError TypeFix
Missing indentation inside loopsIndentationError: expected an indented blockIndent loop body correctly
Extra indentation before a loopIndentationError: unexpected indentRemove unnecessary spaces
Mixing spaces and tabsTabError: inconsistent use of tabs and spacesUse only spaces
Incorrect indentation in nested loopsIndentationErrorEnsure proper indentation of inner loops
Indentation outside loops affecting executionWrong logicVerify indentation carefully

Leave a Reply

Your email address will not be published. Required fields are marked *