Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,236 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!