LearnFrenzy
Sign in Take a free test

Loops · Predict the Output · Question 4

The value after a while loop Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

What is the output of the following Python program?

main.py
n = 10
steps = 0
while n > 0:
    n -= 3
    steps += 1
print(n, steps)
  • A -2 4 Correct
  • B 1 3
  • C 0 4
  • D -2 3
Correct Answer: Option A

n goes 10 → 7 → 4 → 1 → -2. The condition is checked only before each pass, so n overshoots to -2 after 4 steps.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links