Challenge: 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.
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.
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 9.09
Challenge: Simulated Traffic Light
Veeg om het menu te tonen
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.
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.
Oplossing
Bedankt voor je feedback!
single