1

I have a while loop which checks for the rising edge of the variable Accessory2. My code is the following:

            # Check for rising edge of ACC2
    while (currentTime.Value - StartTime) < TimeOutValue:
        if Accessory2.Value == 1:
            Acc2StartTime = currentTime.Value
            print rttPrefix + "ACC2 output to ON"
            break
        yield None
    else:
        print rttPrefix + "No ACC2 output"
        DynamicFlag.Value = -2

However, when the variable Accessory2 goes up, the break condition stops the if statement but the else statement is also executed. Any idea why ?

The output is the following:

*RTT:* ACC2 output to ON
*RTT:* No ACC2 output

2 Answers2

2

Maybe you're just executing (or continuing) the while loop multiple times, and the first execution prints your first line and a subsequent execution prints the second.

tom10
  • 67,082
  • 10
  • 127
  • 137
-2

Your else statement is indented at the same level as your while loop, so thus is not inside it.

James Scholes
  • 7,686
  • 3
  • 19
  • 20
  • 3
    It appears that this was intended. The `else` clause of the `while `statement` should not be executed if the loop was broken out of: http://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement – Waleed Khan Jan 11 '13 at 15:21
  • The question asked why an else statement would run after breaking out of a loop. It runs because A) it's not inside the loop and B) because it's not indented at the same level as the if statement above it. – James Scholes Jan 11 '13 at 15:26
  • 3
    @James The else statement is *not* supposed to run if the loop was broken out of. – Waleed Khan Jan 11 '13 at 15:27
  • 1
    @JamesScholes, this is an while-associated else, not if-associated. It should not be executed when while loop halted with a break statement. – kaspersky Jan 11 '13 at 15:29