-3

I want to make a while loop terminate right when it's false without:

while condition:
    if !condition:
        break
    ...
    if !condition:
        break
    ...
    if !condition:
        break
    ...

eg

while led_blink:
    if !led_blink:
        break
    led.value(1)
    if !led_blink:
        break
    time.sleep(1)
    if !led_blink:
        break
    led.value(0)
    if !led_blink:
        break
    time.sleep(1)

If it helps, I'm using MicroPython for the Raspberry PI pico W

You know what, I'm just gonna join a programming discord

1 Answers1

-2

Without seeing the rest of your code it's difficult to know for sure, but this may be helpful.

conditions = [True, False, True]

while all(conditions):
  print("To infinity and beyond")
  break

Running that you'll notice nothing happens, because there is a False in conditions.

Changing it to True and you'll see To infinity and beyond printed in your terminal. Note: I added the break there just to avoid an infinite loop.

Documentation for all(): https://www.w3schools.com/python/ref_func_all.asp

Leslie Alldridge
  • 1,427
  • 1
  • 10
  • 26