pythonpython-3.xraspberry-pigpiozero

Break a while loop when a button is pressed


I am currently trying to setup this code so that when a button in this case button1 connected to a RPi GPIO runs the function c1 and keeps looping that function until another button button2 is pressed and then it runs the function c2 keep looping in that function.

    #Import RPi GPIO module
    from gpiozero import Button

    #Assign name to buttons GPIO pins
    button1 = Button(2)
    button2 = Button(4)

    def c1():
        while True:
            print('c1')
            grade = float(input('Select grade: '))
            if grade < 10:
            print('Less than 10')
        else:
            print ('invalid input')

   def c2():
       while True:
            print('c2')
            grade = float(input('Select grade: '))
            if grade < 10:
                print('Less than 10')
            else:
                print ('invalid input')

I encountered troubles breaking the function c1, I have tried to add a break in the while when another button is pressed as follows without any luck as the code doesn't stop.

    def c1():
        while True:
            print('c1')
            grade = float(input('Select grade: '))
        if grade < 10:
            print('Less than 10')
        elif button2.is_pressed:
            break

I have also tried this to break the loop but I'm not even sure it is a correct way to do it, anyhow it did not work.

    def c1():
        while True:
            print('c1')
            grade = float(input('Select grade: '))
            if grade < 10:
                print('Less than 10')
            elif button2.is_pressed:
                c1() == False
                break

I am not sure to be correct but I have a feeling that something has to change the function let's assume c1 to False to break the loop. I was hoping that by telling the code once a new button is pressed stop the loop would work but it didn't work out.

What am I missing here?


Solution

  • If this is the exact code, then you may notice that the break is outside the while loop.

    def c1():
        while True:
            print('c1')
            grade = float(input('Select grade: '))
        if grade < 10:
            print('Less than 10')
        elif button2.is_pressed:
            break
    

    Meanwhile, in this code, you need to set the boolean variable before the while loop, then set it to false. You don't need the break if you are using this method because the function will break the while loop once the variable is set to false.

    def c1():
        while True:
            print('c1')
            grade = float(input('Select grade: '))
            if grade < 10:
                print('Less than 10')
            elif button2.is_pressed:
                c1() == False
                break
    

    Try this:

    def c1():
        flag = True
        while flag == True:
            print('c1')
            grade = float(input('Select grade: '))
            if grade < 10:
                print('Less than 10')
            elif button2.is_pressed: #try using if here instead of elif
                flag = False
    

    Or this:

    def c1():
        while True:
            print('c1')
            grade = float(input('Select grade: '))
            if grade < 10:
                print('Less than 10')
            elif button2.is_pressed: #try using if here instead of elif
                break
    

    EDIT: Perhaps you've missed the elif, try using if instead if that doesn't work.

    EDIT 2: Further problem.

    def c1():
        while True:
            print('c1')
            if button2.is_pressed:
                break
            grade = float(input('Select grade: '))
            if grade < 10:
                print('Less than 10')