Why do I get an infinite loop?


#1

Question

Why do I get an infinite loop?

Answer

If you’ve incremented num outside of the while loop, then it will never be reached. When a while loop is entered, it only runs the code indented to be inside of it each time it loops. This means that if you incremented num but didn’t indent it to be in the loop, it never gets executed when the loop runs, and our while condition never becomes false!


FAQ: Learn Python- Loops - While you're at it