K
PRO
Konstantin
asked

Expert
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 == 0is true ifnis exactly 0n > 0is true ifnis positiveormeans 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.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer