Course Content
Python Functions Tutorial
Python Functions Tutorial
Challenge: Traffic lights
Swipe to show code editor
Modify the existing Python program to simulate a traffic light using a generator. The program should display the state of the traffic light ("Red"
, "Yellow"
, "Green"
) for a specified number of cycles:
-
Write a generator
traffic_light_generator
that yields the state of the traffic light ("Red"
,"Yellow"
,"Green"
) in an infinite loop. -
Use this generator to output the state of the traffic light to the console nine times in the program.
-
Example output:
Thanks for your feedback!
Challenge: Traffic lights
Swipe to show code editor
Modify the existing Python program to simulate a traffic light using a generator. The program should display the state of the traffic light ("Red"
, "Yellow"
, "Green"
) for a specified number of cycles:
-
Write a generator
traffic_light_generator
that yields the state of the traffic light ("Red"
,"Yellow"
,"Green"
) in an infinite loop. -
Use this generator to output the state of the traffic light to the console nine times in the program.
-
Example output:
Thanks for your feedback!
Challenge: Traffic lights
Swipe to show code editor
Modify the existing Python program to simulate a traffic light using a generator. The program should display the state of the traffic light ("Red"
, "Yellow"
, "Green"
) for a specified number of cycles:
-
Write a generator
traffic_light_generator
that yields the state of the traffic light ("Red"
,"Yellow"
,"Green"
) in an infinite loop. -
Use this generator to output the state of the traffic light to the console nine times in the program.
-
Example output:
Thanks for your feedback!
Swipe to show code editor
Modify the existing Python program to simulate a traffic light using a generator. The program should display the state of the traffic light ("Red"
, "Yellow"
, "Green"
) for a specified number of cycles:
-
Write a generator
traffic_light_generator
that yields the state of the traffic light ("Red"
,"Yellow"
,"Green"
) in an infinite loop. -
Use this generator to output the state of the traffic light to the console nine times in the program.
-
Example output: