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 trialtyler keene
1,474 PointsCant 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.
def multiply(*args):
product = args * args
return args
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThe parameter *args
will be a list of positional argument values. Wrap your product
statement with a for arg in args:
loop.
tyler keene
1,474 PointsThis 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
Treehouse Moderator 68,457 PointsSuper 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
tyler keene
1,474 Pointstyler keene
1,474 Pointswell..... im still lost. haha.
this didn't work. must not be what you meant?
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsGetting closer. Due to the
return
being inside thefor
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
Remember to
product
return
outside of thefor
loop.product