K
PRO
last year
Konstantincountry asked

Is this an infinite loop? while n == 0 or n > 0:

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Konstantin,

Yes, that condition creates an infinite loop — unless something inside the loop changes n to a negative value.

Let’s break it down:

  • n == 0 is true if n is exactly 0

  • n > 0 is true if n is positive

  • or means the loop will keep running as long as either of those is true

So basically, as long as n is zero or positive, the condition stays true, and the loop won’t stop. That makes it infinite — unless you manually change n inside the loop to something negative.

If your goal is to loop only while n is positive (and stop on zero or anything less), just write:

while n > 0:

Here’s a quick example:

n = float(input())
total = 0

while n > 0:
    total += n
    n = float(input())

print(total)

This will keep adding numbers until the user enters zero or a negative value.

If you have more questions, I am here to help.

Python
This question was asked as part of the Getting started with Python course.