Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Simulated Traffic Light | Getting Started with Asyncio
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python Asyncio Basics

bookChallenge: Simulated Traffic Light

A traffic light system is a classic example of a process that cycles through a set of states—typically "Red," "Green," and "Yellow"—each for a fixed period. In a real-world intersection, the light changes states in a predictable order to manage the flow of vehicles and pedestrians. Modeling this behavior asynchronously allows you to simulate the timed transitions between states without blocking the rest of your application. By using Python's asyncio library, you can create a coroutine that switches the light's state after a delay, mimicking how a real traffic signal operates. This approach demonstrates how asynchronous programming can handle time-based events in a non-blocking way.

Task

Swipe to start coding

Create an asynchronous function that simulates a traffic light cycling through the states "Red," "Green," and "Yellow" in order. Each state should be displayed using a print statement, and the function should wait for a short delay between state changes to mimic real traffic light timing. The function should complete one full cycle through all three states and then return. Use asyncio.sleep to introduce the necessary delays between state changes.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me an example of how to implement this traffic light system using asyncio?

How does asynchronous programming improve the simulation of a traffic light compared to synchronous code?

Can you explain how coroutines work in this context?

close

bookChallenge: Simulated Traffic Light

Swipe to show menu

A traffic light system is a classic example of a process that cycles through a set of states—typically "Red," "Green," and "Yellow"—each for a fixed period. In a real-world intersection, the light changes states in a predictable order to manage the flow of vehicles and pedestrians. Modeling this behavior asynchronously allows you to simulate the timed transitions between states without blocking the rest of your application. By using Python's asyncio library, you can create a coroutine that switches the light's state after a delay, mimicking how a real traffic signal operates. This approach demonstrates how asynchronous programming can handle time-based events in a non-blocking way.

Task

Swipe to start coding

Create an asynchronous function that simulates a traffic light cycling through the states "Red," "Green," and "Yellow" in order. Each state should be displayed using a print statement, and the function should wait for a short delay between state changes to mimic real traffic light timing. The function should complete one full cycle through all three states and then return. Use asyncio.sleep to introduce the necessary delays between state changes.

Solution

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 1. Chapter 3
single

single

some-alt