Question Detail
Question
Analyze the following Python code from page 11:
num = 10; while num > 0: print(num); num = num + 1. What is the error in this code?
Correct Answer
Option C is the correct answer.
Detailed Explanation
The variable
num starts at 10. In each iteration, num is incremented (num = num + 1), so it becomes 11, then 12, and so on. Since num is always increasing, the condition num > 0 will always remain True, creating an infinite loop.
Hint
Pay close attention to how the
num variable is updated and its relationship to the loop condition.