The Python Oracle

My While True loop is getting stuck in python

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping

--

Chapters
00:00 My While True Loop Is Getting Stuck In Python
00:39 Accepted Answer Score 7
00:59 Answer 2 Score 1
01:19 Answer 3 Score 1
01:40 Answer 4 Score 0
01:56 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