简体   繁体   中英

Else statement executed even if break is triggered in while loop

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

也许您只是多次执行(或继续)while循环,而第一次执行将打印您的第一行,而随后的执行将打印第二行。

您的else语句与while循环的缩进级别相同,因此不在其中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM