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

Let'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.

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

5 Answers

were are slices here?

Kevin Brennan
Kevin Brennan
19,920 Points

Hi 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

thanks kevin,this helped

Kevin Brennan
Kevin Brennan
19,920 Points

Hi 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
Khaleel Yusuf
15,208 Points

For me, that code didn't work.

Khaleel Yusuf
Khaleel Yusuf
15,208 Points

Thanks! That helped me a lot!