Python Python Collections (2016, retired 2019) Tuples Packing

Brandon Evans
Brandon Evans
8,154 Points

Confused with this solution

Hello!

I was able to solve this challenge, however I am confused why I am returned a tuple when I call this method, instead of the actual product I expect to see in console.

This is my code:

def multiply(*args):
    product = 1
    for arg in args:
        product *= arg
    return product

numbers = 1, 2, 3, 4, 5, 6

print(multiply(numbers))

When I run this in console, the interpreter returns: (1, 2, 3, 4, 5, 6)

Should I not expect to see a single integer (the product) returned in the console? Or am I misunderstanding something?

Thanks for the help! :)

1 Answer

Steven Parker
Steven Parker
203,717 Points

The packing operator (*) is causing the tuple "number" to be handled as a single item. It would do what you expect if you passed a number of individual arguments (such as "multiply(1, 2, 3, 4, 5, 6)"). But when you pass a tuple in, you get the same tuple out.

But to make the function work on an iterable (like "number"), you'd want to remove the "splat": ("def multiply(args):").

Brandon Evans
Brandon Evans
8,154 Points

OOO! That makes perfect sense! :) I was oblivious to the fact the point of this challenge was to pass in multiple arguments lol. Duh. Perfect -- thanks so much for your explanation!