Challenge: Automated Bus Announcements
Swipe to start coding
If you've ever ridden a bus or tram, you've probably noticed that station announcements are pre-recorded and repeat continuously.
In this task, you'll create a generator function called bus_announcements
to simulate an automated announcement system for bus stops.
Follow these steps:
- Define a function
bus_announcements(stops)
that takes a list of bus stops as an argument. - Use an infinite loop (
while True
) to make the announcements repeat forever. - Inside the loop, use a for loop (
for stop in stops
) to go through each stop in the list. - Use the
yield
keyword to return the message"Next stop: {stop}"
, where{stop}
is replaced by the stop's name.
When you call this generator and use next()
repeatedly, it should cycle through the stops indefinitely.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.35
Challenge: Automated Bus Announcements
Swipe to show menu
Swipe to start coding
If you've ever ridden a bus or tram, you've probably noticed that station announcements are pre-recorded and repeat continuously.
In this task, you'll create a generator function called bus_announcements
to simulate an automated announcement system for bus stops.
Follow these steps:
- Define a function
bus_announcements(stops)
that takes a list of bus stops as an argument. - Use an infinite loop (
while True
) to make the announcements repeat forever. - Inside the loop, use a for loop (
for stop in stops
) to go through each stop in the list. - Use the
yield
keyword to return the message"Next stop: {stop}"
, where{stop}
is replaced by the stop's name.
When you call this generator and use next()
repeatedly, it should cycle through the stops indefinitely.
Solution
Thanks for your feedback!
single