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

multiplying the members of a tuple

Hi guys, I can't seem to figure out this one. Please take a look at my code, and tell me what I'm doing wrong. Here's the Challenge question: Let's play with the *args pattern. Create a function named multiply that takes any number of arguments. Return the product (multiplied value) of all of the supplied arguments. The type of argument shouldn't matter. Slices might come in handy for this one.

Thanks in advance for your help .....

--Conrad

twoples.py
def multiply(*nums):
       product = 1
       for num in nums:
             product = product * nums
       return product

6 Answers

Taylor Schimek
Taylor Schimek
19,318 Points

Need to multiply product by num, not nums.

Give that a shot.

Nataly Rifold
Nataly Rifold
12,432 Points

I have a question, why do I need to set the object product = 1 for my code to work? Why can't it work like this:

def multiply(*nums): for num in nums: total = num * num return total

def multiply(product, *nums):
    factor= product
    for num in nums:
        factor *= num
    return factor

cool ! it works :-) Thanks Taylor.

vikram choudermet
vikram choudermet
5,701 Points

def multiply(*nums): product = 1 for num in nums: product = product * nums return product

still getting error??

Check your indentation for return and change the

product = product * nums to product = product * num

def multiply(factor, *args):

for i in args:
    factor = factor * i
return factor

This may be a stupid question, but how do you know the product = 1?