Bird
0
0

Identify the error in the following nested loop code:

medium📝 Debug Q6 of 15
Python - For Loop
Identify the error in the following nested loop code:
for i in range(2):
    for j in range(3):
    print(i, j)
AThe outer loop is missing a colon (:).
BThe inner loop's print statement is not properly indented.
CThe range function is used incorrectly.
DThe variable names i and j cannot be used together.
Step-by-Step Solution
Solution:
  1. Step 1: Check indentation

    In Python, the body of loops must be indented consistently.
  2. Step 2: Analyze given code

    The print statement inside the inner loop is not indented, causing a syntax error.
  3. Final Answer:

    The inner loop's print statement is not properly indented. -> Option B
  4. Quick Check:

    Ensure all statements inside loops are indented [OK]
Quick Trick: Indent all statements inside loops properly [OK]
Common Mistakes:
MISTAKES
  • Not indenting inner loop body
  • Missing colons after for statements
  • Incorrect use of range function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes