Why doesn’t while True run forever?


Why doesn’t while True run forever?


If we didn’t increment and check the value of count each time the loop runs, it would indeed run infinitely! In this code snippet, we wanted to enter the while loop unconditionally, which is a common need!
Later you’ll learn about the do-while loop, which is more suited for this kind of use, but for now, just know that you can have a loop that would run until some condition inside the loop is met, and then use break to exit.


5 posts were split to a new topic: Why do we still need “break” to exit?

i have written a program to ask customer to enter number from 0 to 9
if the customer does not enter between 0 to 9 , it should prompt to enter again.

even if i am entering number 2 , it is asking to enter again. Please help

why is it even entering the loop when i am entering number less than 9

You could by start inspecting what p contains:


then the next question is, what data type does raw_input give you?

It looks like telling python to treat the string as an integer did wonders.