Flexible Sum Function
Write a function total(*numbers) that accepts any number of arguments and returns their sum, then use it on a line of space-separated numbers.
What the problem means: instead of writing a function that only adds exactly two numbers, use *args so it can add any amount of numbers you give it — 2, 5, or 20.
Approach: *numbers collects every argument passed into total() as a tuple; sum() adds them all up.
Input: One line: space-separated whole numbers.
Output: One line: the sum of all the numbers.
1 2 3 4
10
- 1 <= count of numbers <= 1000
Hint 1
*numbers inside the function definition collects any number of positional arguments into a tuple.
Hint 2
sum() adds up every value in a tuple or list directly.
Hint 3
total(*numbers) — the * here unpacks the numbers list back into separate arguments for the call.
def total(numbers): collects however many arguments are passed into a tuple called numbers, and sum(numbers) adds them all together. Calling total(numbers) on the parsed input list unpacks it back into individual arguments, so the function works no matter how many numbers were on the input line.