Bird
0
0

You want to create a reaction time game that lights an LED randomly between 2 to 5 seconds after starting. Which Python code snippet correctly implements this delay?

hard📝 Application Q8 of 15
Raspberry Pi - LED and Button Projects
You want to create a reaction time game that lights an LED randomly between 2 to 5 seconds after starting. Which Python code snippet correctly implements this delay?
Aimport random import time time.sleep(random.choice([2, 3, 4, 5]))
Bimport random import time time.sleep(random.uniform(2, 5))
Cimport random import time time.sleep(random.random() * 5)
Dimport random import time time.sleep(random.randint(2, 5))
Step-by-Step Solution
Solution:
  1. Step 1: Understand random delay requirements

    The delay must be any float between 2 and 5 seconds.
  2. Step 2: Analyze each option

    import random import time time.sleep(random.uniform(2, 5)) uses random.uniform(2,5) which returns a float in range 2 to 5, perfect for delay. import random import time time.sleep(random.randint(2, 5)) uses randint which returns integers only. import random import time time.sleep(random.random() * 5) produces a float between 0 and 5 seconds, incorrect range. import random import time time.sleep(random.choice([2, 3, 4, 5])) picks fixed integers only.
  3. Final Answer:

    import random import time time.sleep(random.uniform(2, 5)) -> Option B
  4. Quick Check:

    Use random.uniform for float range delays [OK]
Quick Trick: Use random.uniform(min, max) for float delays [OK]
Common Mistakes:
  • Using randint for float delays
  • Using random.random() without scaling properly
  • Choosing fixed values instead of range

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes