简体   繁体   English

如何在python中修复无限循环

[英]How to fix infinite loop in python

I just started with python and RPi. 我刚开始使用python和RPi。 But i stuck :DI have 4 microswitches, and few posibilities (BC1, BC2, BC3... etc). 但我坚持:DI有4个微动开关,几乎没有可能性(BC1,BC2,BC3等)。 For example, if we choose posibility BC1, then microswitch 1 (btn1) and 2 (btn2) must be active, if that case is true, then ledG1 and vazduh are active. 例如,如果我们选择可能性BC1,那么微动开关1(btn1)和2(btn2)必须处于活动状态,如果是,则ledG1和vazduh处于活动状态。 Also I need to have a posibiliti to insert another posibiliti (BC1, BC2, BC3), and untill we insert another posibility first must be active (if requested switches are active). 另外,我还需要有一个可能的位置才能插入另一个位置(BC1,BC2,BC3),直到我们插入另一个位置才必须处于活动状态(如果请求的开关处于活动状态)。

With this code it's working, but ask me only once to insert possibility. 有了这段代码,它就可以了,但是只问我一次插入的可能性。

BC1 = '1'
BC2 = '2'
BC3 = '3'
BC4 = '4'
BC5 = '5'

def compare ():
    while True:
        Barcode = input("Insert barcode: ")
        while Barcode == BC1:
            if GPIO.input(btn1)==0 and GPIO.input(btn2)==0:
                GPIO.output(vazduh, GPIO.HIGH)
                GPIO.output(ledG1, GPIO.HIGH)
                continue
            else:
                GPIO.output(vazduh, GPIO.LOW)
                GPIO.output(ledG1, GPIO.LOW)
                continue                
        while Barcode == BC2:
            if GPIO.input(btn2)==0 and GPIO.input(btn4)==0:
                GPIO.output(vazduh, GPIO.HIGH)
                GPIO.output(ledG3, GPIO.HIGH)
                continue
            else:
                GPIO.output(vazduh, GPIO.LOW)
                GPIO.output(ledG3, GPIO.LOW)
                continue


compare ()

I think you probably wanted to use if instead of while in your first while's scope. 我认为您可能想在第一时间范围内使用if而不是while

while True:
   Barcode = input("Insert barcode: ")
   if Barcode == BC1:
      #Do something #1
   if Barcode == BC2:
      #Do something #2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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