Bird
0
0

Find the bug in this reaction time game code:

medium📝 Debug Q7 of 15
Raspberry Pi - LED and Button Projects
Find the bug in this reaction time game code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN)
start = time.time()
while GPIO.input(22) == GPIO.LOW:
    pass
end = time.time()
print('Reaction time:', end - start)
AButton press logic reversed; should check for HIGH
BGPIO pin 22 not set as output
CMissing GPIO.cleanup() call
DNo bug; code works correctly
Step-by-Step Solution
Solution:
  1. Step 1: Understand button press logic

    Usually, buttons pull GPIO input LOW when pressed if pull-up resistor is used.
  2. Step 2: Check condition in while loop

    The loop waits while input is LOW, meaning it waits while button is pressed, which is opposite of expected wait for press.
  3. Final Answer:

    Button press logic reversed; should check for HIGH -> Option A
  4. Quick Check:

    Button press logic must match GPIO input state [OK]
Quick Trick: Confirm if button press reads LOW or HIGH before coding loop [OK]
Common Mistakes:
  • Confusing LOW and HIGH states for button press
  • Not using pull-up/down resistors
  • Ignoring GPIO cleanup

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes