Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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.

Tehtävä

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.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

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

Pyyhkäise näyttääksesi valikon

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.

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3
single

single

some-alt