Use Python While Loop with Multiple Conditions

Working on a data processing project for a U.S.-based retail analytics company, I needed to keep a Python loop running until two separate conditions were met, one related to data completeness and another to error validation.

That’s when I realized many Python beginners struggle with the while loop with multiple conditions. It’s not because the syntax is hard, but because understanding how logical operators like and, or, and not interact inside a while loop can be tricky.

In this tutorial, I’ll share exactly how I handle multiple conditions in Python while loops. I’ll show you simple, clear examples and a few real-world use cases you can relate to.

What is a Python While Loop?

A while loop in Python repeatedly executes a block of code as long as a given condition is True.

When the condition becomes False, the loop stops running. It’s perfect when you don’t know beforehand how many times you need to repeat an action.

Here’s a basic example:

count = 0
while count < 5:
    print("Count is:", count)
    count += 1

In this example, Python checks the condition count < 5 before each iteration. Once the count reaches 5, the condition becomes False, and the loop ends.

Method 1 – Use the and Operator in a Python While Loop

The and operator ensures that all conditions must be True for the loop to continue. This is useful when you want your loop to stop if any condition fails.

Here’s an example from a data validation scenario:

temperature = 72
humidity = 40

while temperature < 100 and humidity < 60:
    print(f"Temperature: {temperature}°F, Humidity: {humidity}%")
    temperature += 5
    humidity += 3

print("Loop ended because one of the conditions became False.")

You can see the output in the screenshot below.

python while multiple conditions

In this example, the loop continues running only if both temperature < 100 and humidity < 60 are True.

Method 2 – Use the or Operator in a Python While Loop

The or operator allows the loop to continue as long as at least one condition is True. This is helpful when you want to keep the loop running until all conditions fail.

Here’s a practical example:

battery_level = 20
internet_connected = False

while battery_level > 10 or internet_connected:
    print(f"Battery: {battery_level}%, Internet Connected: {internet_connected}")
    battery_level -= 3
    if battery_level < 15:
        internet_connected = True  # Simulate internet reconnecting

print("Loop stopped as all conditions became False.")

You can see the output in the screenshot below.

python while loop multiple conditions

In this example, the loop keeps running as long as either the battery level is above 10% or the internet connection is active.

Method 3 – Use the not Operator in a Python While Loop

The not operator inverts a condition. This means the loop runs while the condition is not True, or simply, while it’s False.

Here’s an example:

data_received = False
attempts = 0

while not data_received and attempts < 5:
    print(f"Attempt {attempts + 1}: Waiting for data...")
    attempts += 1
    if attempts == 3:
        data_received = True  # Simulate data arriving

print("Data received or maximum attempts reached.")

You can see the output in the screenshot below.

while loop with two conditions python

In this case, the loop continues running until either data is received or the number of attempts reaches 5.

Method 4 – Combine and, or, and not Together

You can also combine all three logical operators to create more complex conditions in a Python while loop. This is common in automation scripts or when handling multiple sensors, inputs, or states.

Here’s a complete example:

temperature = 50
humidity = 30
system_online = True

while (temperature < 90 and humidity < 70) or not system_online:
    print(f"System Online: {system_online}, Temp: {temperature}°F, Humidity: {humidity}%")
    temperature += 10
    humidity += 15
    if temperature >= 70:
        system_online = False  # Simulate system going offline

print("Loop exited as conditions no longer met.")

You can see the output in the screenshot below.

python while two conditions

This loop continues as long as either both temperature and humidity are within limits or the system is offline.

Method 5 – Use Multiple Conditions in a Python While Loop with User Input

Sometimes, you may want to keep asking the user for input until they provide valid information.

Here’s a simple example that checks both numeric range and input validity:

while True:
    age = input("Enter your age (18–65): ")
    if age.isdigit() and 18 <= int(age) <= 65:
        print("Valid age entered.")
        break
    else:
        print("Invalid input. Please try again.")

This loop uses multiple conditions to ensure that the user enters a number and that it falls within the allowed range.

Method 6 – Use While Loop with Multiple Conditions and Lists

You can also use multiple conditions to process data from lists. For example, let’s say you’re processing orders from two different U.S. states:

orders_ny = ["Order1", "Order2", "Order3"]
orders_ca = ["OrderA", "OrderB"]

while len(orders_ny) > 0 and len(orders_ca) > 0:
    print(f"Processing {orders_ny.pop(0)} and {orders_ca.pop(0)}")

This loop continues until either of the lists becomes empty, ensuring that both states’ orders are processed in parallel.

Real-World Example – Monitor a Smart Thermostat System

Here’s a more practical example that ties everything together.

Imagine you’re developing a Python program to monitor a smart thermostat system in a U.S. home. The system should run while the temperature is below 75°F and the system is online or until a manual override occurs.

temperature = 68
system_online = True
manual_override = False

while (temperature < 75 and system_online) or not manual_override:
    print(f"Monitoring... Temp: {temperature}°F | System Online: {system_online}")
    temperature += 2
    if temperature >= 72:
        system_online = False  # Simulate system going offline
    if temperature >= 74:
        manual_override = True  # Simulate user override

print("Monitoring stopped due to system conditions.")

This example shows how multiple conditions can help you manage complex real-world logic in Python automation.

Common Mistakes When Using Multiple Conditions in Python While Loops

Here are some common mistakes I’ve seen developers make:

  1. Forgetting parentheses – Always use parentheses when combining multiple conditions for clarity.
  2. Infinite loops – Ensure at least one condition can become False, or your loop will never stop.
  3. Mixing comparison types – Be consistent when comparing strings, integers, or booleans.
  4. Using = instead of == – A common typo that causes logic errors.

Pro Tip – Simplify Complex Conditions

If your loop has too many conditions, consider breaking them into smaller boolean variables. This improves readability and reduces debugging time.

is_temp_ok = temperature < 80
is_humidity_ok = humidity < 60
is_system_active = True

while is_temp_ok and is_humidity_ok and is_system_active:
    print("System running smoothly.")
    break

This approach makes your code cleaner and easier to maintain.

While loops with multiple conditions are incredibly powerful once you understand how Python evaluates logical expressions.

I often use them in automation scripts, data validation, and system monitoring tasks. The key is to plan your conditions carefully and always ensure that at least one condition will eventually turn False to avoid infinite loops.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.