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

Opgave

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.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

bookChallenge: Simulated Traffic Light

Stryg for at vise menuen

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.

Opgave

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.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
single

single

some-alt