Bird
0
0

Identify the error in this code snippet: ```python from gpiozero import TonalBuzzer buzzer = TonalBuzzer(21) buzzer.play('D4:2') buzzer.stop() ```

medium📝 Debug Q6 of 15
Raspberry Pi - gpiozero Library
Identify the error in this code snippet: ```python from gpiozero import TonalBuzzer buzzer = TonalBuzzer(21) buzzer.play('D4:2') buzzer.stop() ```
AIncorrect GPIO pin number used.
BMissing import of time.sleep to allow note duration.
Cplay() method does not exist for TonalBuzzer.
Dstop() should be called before play().
Step-by-Step Solution
Solution:
  1. Step 1: Check play method usage

    play('D4:2') starts playing note D4 for 2 beats but returns immediately.
  2. Step 2: Identify missing delay

    Without sleep, stop() is called immediately, cutting sound short.
  3. Final Answer:

    Missing import of time.sleep to allow note duration. -> Option B
  4. Quick Check:

    Need sleep to hear note before stop [OK]
Quick Trick: Use sleep to wait while note plays before stopping [OK]
Common Mistakes:
  • Calling stop() too soon
  • Wrong pin number assumption
  • Thinking play() is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes