0

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?

floxia
  • 67
  • 1
  • 7

2 Answers2

1

Have you debugged your program? Have you checked that break was called?

It might help you find the problem.

You can also print: button2.is_pressed to find out what's happening.

Ovid
  • 11
  • 6
  • Thanks for your reply, I had done some debugging but not what you have suggested, how could I print the `break` to see if it is called? It might be useful for the next time – floxia Dec 26 '21 at 00:45
  • 1
    You can use a debugger (such as [`pycharm`'s debugger](https://www.jetbrains.com/pycharm/promo/?source=google&medium=cpc&campaign=14124132918&gclid=CjwKCAiAn5uOBhADEiwA_pZwcL3InTlCwLIwLb3fumdLvNytz5IBIsTrqCG6x8mksRdl2sgPXpnOzhoCczMQAvD_BwE) or [vscode's debugger](https://code.visualstudio.com/docs/python/debugging)), or just print out a message before your `break` line – Ovid Dec 26 '21 at 00:46
1

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')
Coderio
  • 429
  • 2
  • 9
  • The last code you posted did the job! I had though as suggested by you to change the `elif` to `if`. Now I would like to ask why do I need two `if` statements? – floxia Dec 26 '21 at 00:42
  • 1
    The `elif` statement works only when the `if` statement met the condition first. It won't work if the `if` statement is not even executed. – Coderio Dec 26 '21 at 01:00
  • Thanks for clarifying this! I have encountered another issue. The code breaks only if I hold the button while inputting the value during the `input` prompt. Is there any way to make the code break even if it's waiting for an input? – floxia Dec 26 '21 at 02:42
  • Does that mean the button is the priority? – Coderio Dec 26 '21 at 04:02
  • I'm not sure if I understood the question 100% but yes when another button is pressed the code needs to stop regardless of anything and jump to the next code. Do you reckon is possible? – floxia Dec 26 '21 at 04:41
  • Yes, I do. You may need to reconstruct the conditions. I've edited (EDIT 2) my answer above for the solution that may help you. – Coderio Dec 26 '21 at 06:02