Bird
Raised Fist0
Raspberry Piprogramming~20 mins

Reaction time game in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Reaction Time Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the output of this reaction time measurement snippet?
Consider this Python code snippet for a reaction time game on Raspberry Pi. What will it print after the user presses Enter?
Raspberry Pi
import time
start = time.time()
input('Press Enter as fast as you can!')
end = time.time()
print(f'Reaction time: {end - start:.3f} seconds')
AReaction time: 1.234 seconds
BReaction time: 0.123 seconds
CReaction time: 0.000 seconds
DSyntaxError
Attempts:
2 left
💡 Hint
The printed time is the difference between when the prompt appears and when Enter is pressed.
🧠 Conceptual
intermediate
1:00remaining
Which Raspberry Pi component is best for detecting a physical button press in a reaction time game?
You want to measure reaction time using a physical button connected to your Raspberry Pi. Which component should you use to detect the button press?
ASPI communication bus
BI2C communication bus
CGPIO pin configured as input with pull-up resistor
DPWM output pin
Attempts:
2 left
💡 Hint
Think about how to read a simple on/off signal from a button.
🔧 Debug
advanced
2:00remaining
Why does this reaction time game code never detect the button press?
This code is supposed to detect a button press on GPIO pin 17 and print reaction time, but it never prints anything. What is the problem?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)

start = time.time()
while True:
    if GPIO.input(17) == GPIO.HIGH:
        end = time.time()
        print(f'Reaction time: {end - start:.3f} seconds')
        break
AThe loop needs a sleep() call to detect input.
BGPIO.setup should use GPIO.OUT for input pins.
Ctime.time() returns a string, causing a TypeError.
DThe button is wired to pull the pin LOW when pressed, so checking for HIGH never triggers.
Attempts:
2 left
💡 Hint
Check how the button wiring affects the signal level when pressed.
📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this Raspberry Pi reaction time code?
This code snippet has a syntax error. Choose the corrected version.
Raspberry Pi
import time
start = time.time()
input('Press Enter')
end = time.time()
print(f'Reaction time: {end - start:.3f seconds')
Aprint(f'Reaction time: {end - start:.3f} seconds')
Bprint(f'Reaction time: {end - start:.3f seconds}')
Cprint('Reaction time: {end - start:.3f} seconds')
D)'}sdnoces f3.:trats - dne{ :emit noitcaeR'f(tnirp
Attempts:
2 left
💡 Hint
Check the placement of the closing brace and parentheses in the f-string.
🚀 Application
expert
3:00remaining
How to implement a non-blocking reaction time game loop on Raspberry Pi?
You want to create a reaction time game that waits a random time, then lights an LED and measures how fast the user presses a button. The program should not freeze while waiting. Which approach is best?
AUse input() to wait for the button press after lighting the LED.
BUse a loop that checks time.time() and GPIO input repeatedly with a small sleep delay.
CUse time.sleep(random_delay) then input() to wait for button press.
DUse a blocking infinite loop without any delay or input.
Attempts:
2 left
💡 Hint
Think about how to keep the program responsive while waiting.

Practice

(1/5)
1. What is the main purpose of a reaction time game on a Raspberry Pi?
easy
A. To measure how quickly a player presses a button after an LED lights up
B. To count how many times a button is pressed in a minute
C. To display random colors on an LED strip
D. To control the brightness of an LED using a sensor

Solution

  1. Step 1: Understand the game goal

    The reaction time game tests how fast a player reacts to a signal, usually an LED lighting up.
  2. Step 2: Identify the key action

    The player must press a button as soon as the LED lights up to measure reaction speed.
  3. Final Answer:

    To measure how quickly a player presses a button after an LED lights up -> Option A
  4. Quick Check:

    Reaction time = button press speed after LED [OK]
Hint: Focus on measuring speed after LED signal [OK]
Common Mistakes:
  • Confusing reaction time with counting presses
  • Thinking the game controls LED brightness
  • Assuming it displays colors randomly
2. Which Python code snippet correctly sets up GPIO pin 17 as an input for a button on Raspberry Pi?
easy
A. GPIO.setup(17, GPIO.OUT)
B. GPIO.setup(17, GPIO.IN)
C. GPIO.input(17, GPIO.IN)
D. GPIO.output(17, GPIO.IN)

Solution

  1. Step 1: Recall GPIO setup syntax

    To set a pin as input, use GPIO.setup(pin_number, GPIO.IN).
  2. Step 2: Match the correct option

    GPIO.setup(17, GPIO.IN) uses GPIO.setup(17, GPIO.IN), which is correct for input pin setup.
  3. Final Answer:

    GPIO.setup(17, GPIO.IN) -> Option B
  4. Quick Check:

    GPIO.setup(pin, GPIO.IN) sets input pin [OK]
Hint: Use GPIO.setup with GPIO.IN for input pins [OK]
Common Mistakes:
  • Using GPIO.OUT instead of GPIO.IN for input
  • Confusing GPIO.input() with GPIO.setup()
  • Trying to use GPIO.output() on input pins
3. What will be the output of this code snippet in a reaction time game?
import time
start = time.time()
# LED lights up here
input('Press Enter as fast as you can!')
end = time.time()
print(f'Reaction time: {end - start:.2f} seconds')
medium
A. No output because input() is not called
B. Reaction time: 0.00 seconds
C. SyntaxError due to missing parentheses
D. Reaction time: time taken between prompt and Enter press

Solution

  1. Step 1: Understand timing with time.time()

    The code records time before and after waiting for user input.
  2. Step 2: Calculate reaction time

    The difference end - start measures how long the user took to press Enter after the prompt.
  3. Final Answer:

    Reaction time: time taken between prompt and Enter press -> Option D
  4. Quick Check:

    Reaction time = end - start after input [OK]
Hint: time.time() difference measures reaction delay [OK]
Common Mistakes:
  • Assuming reaction time is always zero
  • Confusing syntax errors with correct code
  • Thinking input() is not called
4. Identify the error in this Raspberry Pi reaction game code snippet:
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.IN)
GPIO.output(23, GPIO.HIGH)
# Wait for button press on pin 23
medium
A. Pin 23 should be set as GPIO.OUT
B. GPIO.setup() missing for pin 18
C. GPIO.output() used on input pin 23, which is incorrect
D. No error, code is correct

Solution

  1. Step 1: Check pin setup

    Pin 23 is set as input with GPIO.setup(23, GPIO.IN).
  2. Step 2: Identify incorrect usage

    GPIO.output() cannot be used on input pins; it is only for output pins.
  3. Final Answer:

    GPIO.output() used on input pin 23, which is incorrect -> Option C
  4. Quick Check:

    GPIO.output() only for output pins [OK]
Hint: Use GPIO.output only on output pins [OK]
Common Mistakes:
  • Trying to write output on input pins
  • Forgetting to setup pins before use
  • Assuming no error when pin modes mismatch
5. You want to improve your Raspberry Pi reaction time game by adding a random delay before lighting the LED. Which code snippet correctly adds a random delay between 1 and 5 seconds before turning on the LED on pin 18?
hard
A. import random import time wait = random.randint(1, 5) time.sleep(wait) GPIO.output(18, GPIO.HIGH)
B. import random wait = random.random(1, 5) time.sleep(wait) GPIO.output(18, GPIO.HIGH)
C. import random wait = random.randint(1, 5) GPIO.output(18, GPIO.HIGH) time.sleep(wait)
D. import time wait = random.randint(1, 5) time.sleep(wait) GPIO.output(18, GPIO.HIGH)

Solution

  1. Step 1: Use random.randint for integer delay

    random.randint(1, 5) returns an integer between 1 and 5 seconds.
  2. Step 2: Sleep before turning LED on

    time.sleep(wait) pauses the program before GPIO.output(18, GPIO.HIGH) lights the LED.
  3. Final Answer:

    import random import time wait = random.randint(1, 5) time.sleep(wait) GPIO.output(18, GPIO.HIGH) -> Option A
  4. Quick Check:

    random.randint + sleep before LED ON [OK]
Hint: Sleep before LED ON using random.randint delay [OK]
Common Mistakes:
  • Using random.random with arguments (invalid)
  • Sleeping after turning LED on
  • Not importing random module
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard