Editor

Handle Volume Button Events

Beginner · Write the code · from Program Design and Testing: Plan, Build and Debug Code

From the lesson: Where do inputs come from, and where do outputs go?

Input is data sent to a computer for a program to process. Output is any data a program sends to a device, such as a screen, a speaker or a motor.

Read this part of the lesson

A music app starts at volume 5. Write final_volume(events) that takes a list of button events and returns the volume after all of them have been handled:

  • "up" raises the volume by 1, but never above 10.
  • "down" lowers it by 1, but never below 0.
  • "mute" sets it to 0.
  • final_volume(["up", "up", "down"])  ->  6
    def final_volume(events):
    volume = 5
    # Handle each event here
    return volume

    What Check runs

    Your code runs first, then each of these lines, and 2 more that stay hidden. The output must match exactly.

    RunsExpected output
    print(final_volume(["up", "up", "down"]))6
    print(final_volume([]))5
    print(final_volume(["mute", "up"]))1

    More Program Design exercises

    All Python exercises · 50 practice problems