The Python Oracle

My While True loop is getting stuck in python

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Intrigue Looping

--

Chapters
00:00 Question
01:05 Accepted answer (Score 7)
01:38 Answer 2 (Score 1)
02:05 Answer 3 (Score 1)
02:28 Answer 4 (Score 0)
02:52 Thank you

--

Full question
https://stackoverflow.com/questions/3099...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #whileloop #raspberrypi #infiniteloop #raspbian

#avk47



ACCEPTED ANSWER

Score 7


You need to keep updating your input_* variables inside your while loop

while True:
    input_A = GPIO.input(26)
    input_B = GPIO.input(19)
    input_C = GPIO.input(13)
    input_D = GPIO.input(6)

    if input_A == True:
            print('A was pushed')

    if input_B == True:
            print('B was pushed')

    if input_C == True:
            print('C was pushed')

    if input_D == True:
            print('D was pushed')

    sleep(1.5);



ANSWER 2

Score 1


At the break statement under each if-statement. While you're at it, change the 2nd to last ifs to elifs.

while True:
    if input_A == True:
        print('A was pushed')
        break
    elif input_B == True:
        print('B was pushed')
        break
    elif input_C == True:
        print('C was pushed')
        break
    elif input_D == True:
        print('D was pushed')
        break



ANSWER 3

Score 1


When you declare

input_A = GPIO.input(26)
input_B = GPIO.input(19)
input_C = GPIO.input(13)
input_D = GPIO.input(6)

you are assigning a value to those variables that will not change, because you are not updating them inside the loop.

Therefore, you need to add a line in the loop that updates the inputs A B C and D.




ANSWER 4

Score 0


why don't you try introducing a break for each if statement. It should stop it from looping infinitely.

and updating your variables i.e the inputs should be in the while loop.

e.g

if input_A == True:
    print('A was pushed')
    break