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 trialConrad Landicho
3,312 Pointsmultiplying 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
def multiply(*nums):
product = 1
for num in nums:
product = product * nums
return product
6 Answers
Taylor Schimek
19,318 PointsNeed to multiply product by num, not nums.
Give that a shot.
Nataly Rifold
12,432 PointsI 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
Gerard Nwazk
Courses Plus Student 2,833 Pointsdef multiply(product, *nums):
factor= product
for num in nums:
factor *= num
return factor
Conrad Landicho
3,312 Pointscool ! it works :-) Thanks Taylor.
vikram choudermet
5,701 Pointsdef multiply(*nums): product = 1 for num in nums: product = product * nums return product
still getting error??
Steef Vendy
977 PointsCheck your indentation for return and change the
product = product * nums to product = product * num
Prince Vonleh
Courses Plus Student 4,552 Pointsdef multiply(factor, *args):
for i in args:
factor = factor * i
return factor
Alexander Kim
1,243 PointsThis may be a stupid question, but how do you know the product = 1?