Search⌘ K
AI Features

Challenge: Working with Coroutines

Explore how to create Python coroutines that maintain a running average of numbers and build a coroutine-based log processing pipeline. Understand coroutine mechanics like sending values, stopping with None, and chaining coroutines for asynchronous workflows in this practical challenge.

We'll cover the following...

Problem 1

In this problem, you are required to implement a coroutine running_average() that calculates the running average of numbers sent to it.

Requirements

  • The coroutine should:
    1. Start with no numbers.
    2. Accept numbers using .send(value).
    3. After each number is sent, yield the current running average.
  • If .send() is called with None, the coroutine should stop and return the final average.
...