Multiple while loop in a single program

How to use more than one while loop in a python program, to serve 2 different services. I tried to apply while loop but while it executes only the first while loop, it’s also not showing any indentation error.
Here is my code:



You may check this link for multiple while loops:

Hii @11170021
Statements within a while True block continue endlessly and hence the code gets stuck there. If you want to run 2 while loops in the same code, then you’ll have to initiate while loops with a condition. For example:

Capture
As you can see the while loop will terminate after 5 iterations. A similar condition satisfying your need will repair you code. The current problem with your code is that after your code enters the second while loop, there is no break for it, so it continues forever.

Hope this helps! :smiley:

Python While Loop Multiple Conditions To combine two conditional expressions into one while loop, you’ll need to use logical operators. Using 2 while loops in one code u have to mention conditions.so it can run without mentioning it will run endlessly…

In your code in the first while loop, there is a break statement only on the first if statement inside the loop. As you have given while True it will be an infinite loop. To execute the second loop you will need to put break statements in the first while loop to stop it when the desired action is finished. If this does not suit what you are trying to do then you can give conditions in the while statement instead of True to terminate the loop.

While loop continues to run as long as the condition given is satisfied. In both the while loops that you have mentioned in the program have the mentioned condition as ‘true’. This is not enough. Since, the condition mentioned both the times is same only one loop runs and then the program stops. You need to initiate two valid conditions in both the cases.

  1. Create two functions, one for each service:
  • A function to check the weather and send an SMS message to the user.
  • A function to monitor the weather conditions and send a signal to the relay module to turn off electrical appliances if there is a thunderstorm.
  1. Place the two functions within a single parent while loop, with the parent while loop having a condition that is always true.
  2. Start the parent while loop.

This will ensure that the two services are executed sequentially, but they will still be able to serve two different tasks: checking the weather and sending an SMS message to the user, and monitoring the weather conditions and sending a signal to the relay module to turn off electrical appliances if there is a thunderstorm.

If you want to use more than one while loop to serve two different services in a Python program, you can do so by structuring your code appropriately. Each while loop should have its own condition for termination and should contain the code for its respective service. Here’s a basic example of how to use two while loops in a Python program:

# Service 1
while condition_for_service_1:
    # Code for service 1
    # ...
    
# Service 2
while condition_for_service_2:
    # Code for service 2
    # ...

Make sure that the conditions for each while loop are distinct so that each loop runs independently based on its own condition. If you find that only the first while loop is executing and the second one is not, consider checking the following:

  1. Condition: Ensure that the condition for the second while loop is correctly specified and evaluates to True when the loop should run.
  2. Indentation: Make sure that the code within each while loop is properly indented. Python relies on indentation to determine the scope of code blocks.
  3. Loop Termination: Ensure that there is a way for the second while loop to eventually terminate. If the condition is never met, the loop will run indefinitely.

Here’s a more concrete example:

# Service 1: Count from 1 to 5
count_service_1 = 1
while count_service_1 <= 5:
    print(f"Service 1: Count is {count_service_1}")
    count_service_1 += 1

# Service 2: Count from 10 to 15
count_service_2 = 10
while count_service_2 <= 15:
    print(f"Service 2: Count is {count_service_2}")
    count_service_2 += 1

In this example, both while loops have their own conditions and code blocks, and they run independently to provide two different services.

In your code in the first while loop, there is a break statement only on the first if statement inside the loop. As you have given while True it will be an infinite loop. To execute the second loop slope unblocked you will need to put break statements in the first while loop to stop it when the desired action is finished. Simple answer is NO you can’t . because once you enter the first while true loop you will never leave it , unless you specifically program it to break from the loop and enter the second while true loop but then you have the same problem and continually breaking out of a while true loop is not a good way to program.

In your code in the first while loop, there is a break statement only on the first if statement inside the loop. As you have given while True it will be an infinite loop. To execute the second loop you will need to put break statements in the first while loop to stop it when the desired action is finished.

Using 2 while loops in one code u have to mention conditions.so it can run without mentioning it will run endlessly… In your code in the first while loop, there is a break statement only on the first if statement inside the loop. As you have given while True it will be an infinite loop.