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!
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
Where does slices come into play in this task. I keep getting 'try again' messages
def multiply(base, *args): num = base for value in args: num = value* return num
Philip Schultz11,437 Points
Hey, just accept *args as a parameter. then declare the base inside and make it equal to 1 at first. Like so.
def multiply(*args): base = 1 for item in args: base *= item return base
I'm still trying to figure out how to use it with slices also.....
I'm not sure where slices come into play, but this challenge can be accomplished using an accumulating variable holding the current product, and every iteration as you loop through the numbers, you multiply the accumulating variable by it.
def multiply(*nums): acc = 1 for num in nums: acc *= num return acc
Or, if you like using FP (Functional Programming), you can use the
reduce method from
from functools import reduce def multiply(*nums): return reduce(lambda x, y : x * y, nums)