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

tyler keene
tyler keene
1,474 Points

Cant seem to figure this out :(

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.

this is my first time posting. Ive been able to find answers and hints for all other questions up to this point so far, but not this one :( please advise.

twoples.py
def multiply(*args):
    product = args * args
    return args

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

The parameter *args will be a list of positional argument values. Wrap your product statement with a for arg in args: loop.

tyler keene
tyler keene
1,474 Points

well..... im still lost. haha.

def multiply(*args):
    for arg in args:
        return arg * arg

this didn't work. must not be what you meant?

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Getting closer. Due to the return being inside the for loop, your latest code returns the squared value of the first argument at the end of the first iteration. You will need to accumulate all the args into the product value.

Try using something like

product = product * arg
# or
product *= arg

Remember to

  • set the initial value for product
  • move return outside of the for loop.
  • return product
tyler keene
tyler keene
1,474 Points

This is really escaping after i was away a couple weeks.

def multiply(*args):
    for arg in args:
        product *= arg
    return product

am i getting closer, or farther away?

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Super close!

# Remember that
product *= arg

# is the same as 
product = product * arg

Tto avoid a "product referenced before assignment" error, `product needs to a initially assigned:

# add before loop
product = 1  # multiplicative identity