Challenge - 5 Problems
Traffic Light Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple traffic light sequence
What will be the output of this Raspberry Pi traffic light simulation code snippet?
Raspberry Pi
import time lights = ['Red', 'Green', 'Yellow'] for light in lights: print(f"Light is {light}") time.sleep(1)
Attempts:
2 left
💡 Hint
Look at the order of the lights in the list and how the loop prints them.
✗ Incorrect
The code loops through the list in order: Red, Green, then Yellow, printing each light.
🧠 Conceptual
intermediate1:30remaining
Understanding GPIO pin setup for traffic lights
Which statement correctly describes the purpose of setting GPIO pins as outputs in a Raspberry Pi traffic light program?
Attempts:
2 left
💡 Hint
Think about what output pins do in controlling LEDs.
✗ Incorrect
Output pins send electrical signals to devices like LEDs to control their state.
🔧 Debug
advanced2:30remaining
Identify the error in this traffic light timing code
What error will this Raspberry Pi traffic light code produce when run?
Raspberry Pi
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) try: while True: GPIO.output(17, True) time.sleep(2) GPIO.output(17, False) time.sleep('2') finally: GPIO.cleanup()
Attempts:
2 left
💡 Hint
Check the argument types passed to time.sleep.
✗ Incorrect
time.sleep() requires a number (int or float), but '2' is a string causing a TypeError.
📝 Syntax
advanced2:00remaining
Correct syntax for turning on multiple LEDs
Which option correctly turns on GPIO pins 17, 27, and 22 to simulate a traffic light?
Attempts:
2 left
💡 Hint
Check how GPIO.output accepts pin numbers.
✗ Incorrect
RPi.GPIO.output accepts a single pin or a list/tuple of pins to set multiple at once. A uses a list, correct. B uses set, unsupported (TypeError). C works but multiple calls. D invalid.
🚀 Application
expert3:00remaining
Calculate total cycle time of traffic light simulation
Given this traffic light timing code, what is the total time in seconds for one full cycle?
Raspberry Pi
import time sequence = [ ('Red', 5), ('Green', 3), ('Yellow', 2) ] def run_cycle(): for color, duration in sequence: print(f"Light: {color}") time.sleep(duration) run_cycle()
Attempts:
2 left
💡 Hint
Add the durations for all colors in the sequence.
✗ Incorrect
The total cycle time is 5 + 3 + 2 = 10 seconds.