Question Detail

BIO 107 While Loops & Control Flow Objective Question

Question

Analyze the following Python code from the "Can you find the error?" section:
num = 10
while num > 0:
    print(num)
    num = num + 1

What type of error does this code contain?
Options
A A syntax error, as <code>num = num + 1</code> is incorrect.
B A logical error resulting in the loop never starting.
C A runtime error causing the program to crash immediately.
D A logical error resulting in an infinite loop.
Correct Answer
Correct Answer

Option D is the correct answer.

Detailed Explanation

The goal is to print 10 to 1, but num starts at 10 and increases (num = num + 1). This means num will always be greater than 0, making the condition num > 0 perpetually True, leading to an infinite loop.

Hint

Trace the value of num with each iteration. Does it ever become 0 or less?

Question Info