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 trialTawanda Makosa
1,165 PointsLet's play with the *args pattern. Create a function named multiply that takes any number of arguments. Return the produ
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.
def multiply(*nums):
product = 1
for num in nums:
product = product * num
return product
5 Answers
Raj Dhakad
Courses Plus Student 96 Pointswere are slices here?
Kevin Brennan
19,920 PointsHi Khaleel,
Sorry, for some reason I put result in the loop rather than product! Correct code below:
def multiply(*args):
product = 1
for arg in args:
product *= arg
return product
Tatenda Shangwa
8,710 Pointsthanks kevin,this helped
Kevin Brennan
19,920 PointsHi Tawanda,
You are very close to completing the challenge, if you look at your for loop you see that you have placed your return in the loop, this means that after the first iteration through your function will return and finish. All you need to do is place your return outside the loop as follows:
def multiply(*args):
product = 1
for arg in args:
product *= arg
return product
I would also try to keep to using *args as that is best practice.
Regards Kevin
Khaleel Yusuf
15,208 PointsFor me, that code didn't work.
Khaleel Yusuf
15,208 PointsThanks! That helped me a lot!