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 trialkyle gonzalez
3,965 PointsWhen I compile my answer it doesn't run; when I run it in my text editors it compiles. Any insight into other solutions?
def multiply(*args): f = lambda a,b: a*b product = reduce(f, *args) return product
def multiply(*args):
f = lambda a,b: a*b
product = reduce(f,*args)
return product
1 Answer
Steven Parker
231,275 PointsI can see two issues:
- you need "
from functools import reduce
" - there should be no "splat" (scatter operator) in the call to reduce
I'm surprised you didn't get any errors!
kyle gonzalez
3,965 PointsAh so I was missing the reduce function from the functools library?
When I compile the code in: Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux
With * operator, it returns the desired value (24) from a list of [1,2,3,4]. Without the * operator, it returns [1,2,3,4]. In the Treehouse editor, you are correct: The code is accepted without the * operator. Could you provide some more insight as to why it is not needed? Thank you!
Steven Parker
231,275 PointsThe scatter operator converts the list into individual arguments, but the "reduce" function expects a list as the 2nd argument.
On the other hand, the "multiply" function itself is intended to take "any number of arguments" and multiply them. So you would not pass it a list. A test call might look like this: "multiply(1, 2, 3, 4)
".
Be careful about testing a challenge in an external REPL.
If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.
kyle gonzalez
3,965 Pointskyle gonzalez
3,965 PointsProblem: 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.