Skip to content
Home » Infinite Loops in Python

Infinite Loops in Python

Spread the love

If you haven’t read my previous articles about the for loop and the while loop, feel free to do so. Today we’ll be talking about infinite loops.

It may happen that the condition in the while statement is always true. Then the loop never stops. We call such loops infinite loops. Sometimes such loops are useful, for example in client/server programming, but oftentimes they’re a result of our mistake. If we happen to be trapped in such a loop, we can use the Keyboard Interrupt combination (Ctrl + C) to get out of it. Here’s an example of such an infinite loop:

 number = 1
 while number == 1:
     x = input("Enter a number: ")
     print(x) 

As number is equal to 1 all the time (its value isn’t changed inside the loop), the condition is always true and the loop is infinite.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply