Editor

Flexible Calculator

Advanced · Write the code · from Python *args, **kwargs, Default Values, and Keyword Arguments

From the lesson: Python *args, **kwargs, Default Values, and Keyword Arguments

Think about ordering coffee. You always need to pick a size - that's required. But extras like oat milk, an extra shot, or caramel drizzle? Those are optional. You only mention them when you want them.

Read the lesson

Write a function called calc that takes an operation string ('add', 'multiply', or 'max') and then any number of numeric arguments using *args. It should return the result:

  • 'add': sum of all numbers (0 if none)
  • 'multiply': product of all numbers (1 if none)
  • 'max': largest number (0 if none)
  • Examples: calc('add', 1, 2, 3) returns 6. calc('multiply', 2, 3, 4) returns 24.

    # Define your function here
    # Test it
    print(calc('add', 1, 2, 3))
    print(calc('multiply', 2, 3, 4))
    print(calc('max', 5, 2, 8, 1))

    What Check runs

    Your code runs first, then each of these lines. The output must match exactly.

    RunsExpected output
    print(calc('add', 1, 2, 3))6
    print(calc('multiply', 2, 3, 4))24
    print(calc('max', 5, 2, 8, 1))8

    More Function Arguments exercises

    All Python exercises · 50 practice problems · Functions quiz