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.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Génial!
Completion taux amélioré à 9.09
Challenge: Simulated Traffic Light
Glissez pour afficher le 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.
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
Merci pour vos commentaires !
single