-2

I have the following code:

def begin_time(time):
    print(time)

from gpiozero import Button

begin = Button(27)

I want to call this function with time = 5

Any help would be appreciated

0

A lot depends on what you want the program to do while waiting for the button to be pressed. Button abilities here

You could try (untested code though):

from gpiozero import Button
import time

def begin_time(time):
    print(time)


begin = Button(27)

# Loop doing nothing till the button is pressed        
while being.is_held == False:
   time.sleep(0.01)

# Button pressed so call function
begin_time(5)
| improve this answer | |
2

You can create a function that takes no parameters:

def begin_time_5():
    begin_time(5)

begin.when_pressed = begin_time_5

This is called "currying".

Alternatively, use a lambda:

begin.when_pressed = lambda: begin_time(5)
| improve this answer | |
1

Well if it’s tight you want:

def begin_time(time = 5):
    print(time)

begin.when_pressed = begin_time

Personally, I find this easier to understand than the more pythonic lambda :-) but have to say the lambda version has more flexibility for changing the value.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.