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 lessonWrite 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.
| Runs | Expected 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
- Predict the Keyword CallBeginner
- Greeting with a DefaultBeginner
- Sum All NumbersIntermediate
- Fix the Mutable DefaultIntermediate
- Build a Profile StringIntermediate
- Refactor to Use **kwargsAdvanced
All Python exercises · 50 practice problems · Functions quiz