-1

I am trying to break 2 loops in Python, with one loop inside the other. Of course, when you use the break function in a loop it will only break the loop it's in. Here's the code:

while True:   #Want to break this
    x, y, z = sense.get_accelerometer_raw().values()
    x = abs(x)
    y = abs(y)
    z = abs(z)
    sense.show_message(strftime('%H:%M', gmtime()), scroll_speed= 0.05, text_colour=[0, 255, 0], back_colour=[255, 0, 0])
    if x > 2 or y > 2 or z > 2 :   #Want to break this as well
        break #Break goes here??

I want to break the loop started on the top line and the loop started in line 7. How?

CDspace
  • 2,639
  • 18
  • 30
  • 36
Bill Reason
  • 391
  • 1
  • 5
  • 13

1 Answers1

0

if is not a loop. If is a statement that takes a boolean (true or false) and executes the code depending on the result.

That being said, what you have written will work.

if x > 2 or y > 2 or z > 2 :   #Want to break this as well
    break #Break goes here??

This code will break the while True loop if x > 2 or y > 2 or z > 2.

owenbradstreet
  • 128
  • 1
  • 1
  • 8