Python3 - Why loop doesn't work?

In Python, using loops, we can execute a statement or a group of statements multiple times. Usually, the loop works for all conditions, but there are certain scenarios where the functionality of loops appears to be "not working" due to delays, skipping iterations, or getting stuck in infinite loops.

In this article, we will understand those scenarios with examples.

When sleep() Method is Used Inside Loop

The sleep() method is defined in the time module and used to suspend or stop the execution of the program for a specified number of seconds.

When the sleep() method is placed inside a loop, it suspends the execution of each iteration for the given time. After the delay, the loop continues to execute as usual, processing the next iteration sequentially.

Example

In the following example, the for loop appears to be "not working" because it's suspended for 1 second for each iteration ?

import time

for i in range(3):
    print("Hello welcome to Tutorialspoint.")
    time.sleep(1)  # suspended for one second
    print("Delay of 1sec")

Following is the output of the above code ?

Hello welcome to Tutorialspoint.
Delay of 1sec
Hello welcome to Tutorialspoint.
Delay of 1sec
Hello welcome to Tutorialspoint.
Delay of 1sec

When 'continue' Statement Used Inside a Loop

The continue statement is used to skip the execution of the current iteration based on a condition; it returns control to the beginning and starts the next iteration.

If we place the continue statement within the for loop, certain iterations will be skipped. We can suspend a particular iteration by using the continue statement within an if block.

Example

Here, the for loop executes for all iterations but skips the iteration where i is equal to 2 ?

for i in range(5):
    if i == 2:
        continue
    print(i)

Following is the output of the above code ?

0
1
3
4

When Infinite Loop Occurs

An infinite loop occurs when the loop condition never becomes false, causing the loop to run indefinitely. This makes it appear as if the loop is "not working" because the program becomes unresponsive.

Example

Here's an example of an infinite while loop that will run forever ?

# WARNING: This creates an infinite loop
i = 0
while i < 5:
    print(f"Current value: {i}")
    # Missing i += 1, so condition never changes

This loop never terminates because i remains 0, and the condition i < 5 is always true.

When 'break' Statement Used Inside a Loop

The break statement terminates the loop prematurely when a certain condition is met. This can make it appear that the loop isn't working as expected.

Example

Here, the loop appears to stop early because of the break statement ?

for i in range(10):
    if i == 3:
        break
    print(i)

Following is the output of the above code ?

0
1
2

Conclusion

Python loops may appear to "not work" due to delays from sleep(), skipped iterations with continue, premature termination with break, or infinite loops. Understanding these behaviors helps debug loop-related issues effectively.

Updated on: 2026-03-25T04:59:14+05:30

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements