0
0
Raspberry Piprogramming~20 mins

Button class with callbacks in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Button Callback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when a button press triggers the callback?

Consider a Button class that calls a callback function when pressed. What will be printed when the button is pressed?

Raspberry Pi
class Button:
    def __init__(self, callback):
        self.callback = callback
    def press(self):
        self.callback()

def greet():
    print("Button pressed!")

btn = Button(greet)
btn.press()
ANo output
BError: callback not defined
CButton pressed!
DButton pressed! Button pressed!
Attempts:
2 left
💡 Hint

Think about what the press method does with the callback.

🧠 Conceptual
intermediate
1:30remaining
Which statement about callbacks in a Button class is true?

In a Button class that accepts a callback function, which of the following is true about how callbacks work?

ACallbacks can only be functions defined inside the Button class.
BThe callback function runs immediately when the Button object is created.
CThe callback function must return a value to work properly.
DThe callback function is stored and called when the button is pressed.
Attempts:
2 left
💡 Hint

Think about when the callback is executed.

🔧 Debug
advanced
2:30remaining
Why does this Button callback code raise an error?

What error does the following code raise when btn.press() is called?

Raspberry Pi
class Button:
    def __init__(self, callback):
        self.callback = callback
    def press(self):
        self.callback(self)

def greet():
    print("Hello")

btn = Button(greet)
btn.press()
ATypeError: greet() takes 0 positional arguments but 1 was given
BNameError: name 'greet' is not defined
CAttributeError: 'Button' object has no attribute 'callback'
DNo error, prints 'Hello'
Attempts:
2 left
💡 Hint

Check how the callback is called and how it is defined.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a Button class with a callback that accepts an argument?

Choose the correct Button class definition where the callback function accepts the Button instance as an argument.

A
class Button:
    def __init__(self, callback):
        self.callback = callback
    def press(self):
        self.callback(self.callback)
B
class Button:
    def __init__(self, callback):
        self.callback = callback
    def press(self):
        self.callback(self)
C
class Button:
    def __init__(self, callback):
        self.callback = callback
    def press(self):
        self.callback(self, self)
D
class Button:
    def __init__(self, callback):
        self.callback = callback
    def press(self):
        self.callback()
Attempts:
2 left
💡 Hint

The callback should be called with exactly one argument: the Button instance.

🚀 Application
expert
3:00remaining
What is the output of this Button class with multiple callbacks?

Given the code below, what is printed when btn.press() is called?

Raspberry Pi
class Button:
    def __init__(self):
        self.callbacks = []
    def add_callback(self, cb):
        self.callbacks.append(cb)
    def press(self):
        for cb in self.callbacks:
            cb(self)

def cb1(btn):
    print("First callback")

def cb2(btn):
    print("Second callback")

btn = Button()
btn.add_callback(cb1)
btn.add_callback(cb2)
btn.press()
AFirst callback\nSecond callback
BFirst callback
CSecond callback\nFirst callback
DNo output
Attempts:
2 left
💡 Hint

Callbacks are called in the order they were added.