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

Nikki Wong
Nikki Wong
9,066 Points

*args , what am I doing wrong? surely this should give me the right answer?

So I need to get product of all supplied argument. *args packs everything into tuple so its immutable... so I make it to a list and copied it (using slice as it was mentioned.. but don't see the need??), and then get each item from tuple and multiply them together??

twoples.py
def multiply(*args):
    args = list(args)
    argscopy = args[:]
    product=None
    for data in argscopy:
        product *= data

    return product

1 Answer

Steven Parker
Steven Parker
229,732 Points

You don't want to perform math on None, just like you would not want to multiply by 0. But if you start the "product" with 1 you should get the correct answer.

And it's not necessary to copy or convert "args", you can use it directly as the loop iterable. (The suggestion about using a slice refers to a different method of calculating the result than the one you are using).

Nikki Wong
Nikki Wong
9,066 Points

I tried 1, but it said that argument can be any data type, so assuming its a list of words, then how am I suppose to keep multiplying?

eg. input is "apple", "bat", "c, 3, 4 ... etc so product = 1*apple product = apple product = apple*bat?

I dont think you can multiply words?

Steven Parker
Steven Parker
229,732 Points

I'm not sure what the significance of the instruction "The type of argument shouldn't matter." is. But I think you can rely on the data not containing consecutive strings, otherwise the instructions would have been explicit about what to do in such a situation. As you point out, that doesn't make sense in itself.

My suggestion about the initialization of "product" is enough in itself to pass the challenge, but you could optionally make your code more concise if you also apply the second suggestion.