Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Automated Bus Announcements | Function Return Value Specification
Python Functions Tutorial

bookChallenge: Automated Bus Announcements

Task

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:

  1. Define a function bus_announcements(stops) that takes a list of bus stops as an argument.
  2. Use an infinite loop (while True) to make the announcements repeat forever.
  3. Inside the loop, use a for loop (for stop in stops) to go through each stop in the list.
  4. 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 5
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 4.35

bookChallenge: Automated Bus Announcements

Swipe to show menu

Task

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:

  1. Define a function bus_announcements(stops) that takes a list of bus stops as an argument.
  2. Use an infinite loop (while True) to make the announcements repeat forever.
  3. Inside the loop, use a for loop (for stop in stops) to go through each stop in the list.
  4. 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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 5
single

single

some-alt