Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Traffic lights | Function Return Value Specification
Python Functions Tutorial
course content

Course Content

Python Functions Tutorial

Python Functions Tutorial

1. What is a Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

book
Challenge: Traffic lights

Task
test

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:

  1. Write a generator traffic_light_generator that yields the state of the traffic light ("Red", "Yellow", "Green") in an infinite loop.

  2. Use this generator to output the state of the traffic light to the console nine times in the program.

  3. Example output:

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 5
toggle bottom row

book
Challenge: Traffic lights

Task
test

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:

  1. Write a generator traffic_light_generator that yields the state of the traffic light ("Red", "Yellow", "Green") in an infinite loop.

  2. Use this generator to output the state of the traffic light to the console nine times in the program.

  3. Example output:

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt