ESCAPE INFINITE LOOP
Have you ever seen a GIF that loops repeatedly, like the one above? How do you feel? At first, you might find it amusing or laugh at it. But after a few moments, it becomes annoying. It feels like you’re trapped in something, and you don’t want to see the meme anymore. This same feeling can occur when you get stuck in an infinite loop in Python.
When you start learning Python, you’ll discover you can ask Python to execute a block of code repeatedly without writing each cycle individually. This is called a looping function. For basic looping, we can use either a for loop or a while loop. There is a difference in their usage. In a while loop, you can easily create an infinite loop, unlike in a for loop. This is because, in a for loop, you must define the start, end, and step, whereas in a while loop, you don’t have to declare these.
Here’s an example of how you can create an infinite loop in Python using while True.
Input:
Output:
In the example above, the output will print ‘Hello World’ forever unless you force the system to stop by pressing ctrl + c. This code creates an infinite loop because a while loop runs as long as its condition remains True. Since True is a constant Boolean value, the loop will never exit on its own.
So, why would you get frustrated if you are trapped in an infinite loop? There are some consequences of an infinite loop. Firstly, an infinite loop causes your program to get stuck, preventing any further code from executing. Secondly, it can cause your system to freeze or crash as it continuously consumes CPU and memory without an exit condition. Thirdly, if this happens in a large or complex program, it will slow down your computer.
If infinite loops can be so frustrating, why do people still use them? Even though infinite loops may seem problematic, they are useful when you need a system to run continuously. One example is keeping a program running until a user provides an exit condition. An infinite loop guarantees that the system will run continuously without you having to declare the path. However, due to the risks, you need to make sure to create a way to exit the loop to avoid getting trapped. The only way to exit an infinite loop is from within the loop itself, using the break to stop execution manually and immediately.
Input:
Output:
In the example above, the function “process_items” contains a while loop. When return False is executed, it not only exits the function but also stops the infinite loop because the function call ends, preventing further execution.
Get Christinsaktiani’s stories in your inbox
Join Medium for free to get updates from this writer.
If you create an infinite loop inside a function (def), you can use return False to exit the function, effectively stopping the loop.
Input:
Output:
In the above code, you define a function called “process_items” that contains while loop inside of the function. With return False, you not only exit the function but also exit the infinite loop inside the function because the condition of the while loop isn’t fulfilled, which is False. This way, you could escape from the trap of an infinite loop.
But, honestly, even though you know how to escape from an infinite loop, you still must be careful on using the infinite loop. Just use an infinite loop when you need it and know how to escape from the trap, if you are not sure, just declare the exit condition like the example below.
Input:
Output:
In the above example, we can see that declaring the True condition will help you to avoid an infinite loop. An infinite loop may be helpful, but it still has a high risk to out of control and trapping you. Besides, it’s hard to detect infinite loops inside Python unless you execute them. But it will crash your code. In my experience, as a new learner in Python, True looks helpful, especially for making a simple app that needs user input to do an exit action. But I realize it’s really hard to understand how to escape from being trapped especially when you use many layers of nested infinite loop. I’ve learned it in a hard way how to escape from that. So, if you don’t need to make an infinite loop, it will be wiser if you don’t have to.
Hopefully, this sharing could help anyone who also faces the same difficulties as me. It may feel frustrating, like seeing Patrick’s meme earlier, but when you understand more and more deeply, you will find the excitement. Happy learning!







