简体   繁体   中英

How to make multiple time while loop Raspberry Pi? Python

Can I use the while loop multiple times? And how could I do that? For example, in this code I want it to detect the string "turn left". If it does the button will send a signal, if it is pressed. My question now is how I can make the loop check for other strings like "turn right", "take the first exit" at the same time?

while instruction == "turn left":

    if (GPIO.input(12) == False):
        print("button press")
        assistant.start_conversation()
        break

    else:
        GPIO.output(3, GPIO.HIGH)
        sleep(0.3)
        GPIO.output(3, GPIO.LOW)
        sleep(0.3)

There are multiple things you could do; for example running the while-loop indefinitely and checking for the instruction with the "if" comparison-operator.

while True:
    if instruction == "turn left":
        //do something
    if instruction == "turn right":
        //do something

Or you could use multithreading, which allows you to have mulitple while-loops running in parallel.

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