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
wayne ross3,225 Points
I am not thinking broad enough but how would using slices help me out? Also this code is not working on some input?
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.
I tested this out with numbers for the input that works i believe but didnt find any ways to search for numbers in tuples where I could slice that value out for the arithmetic. Honestly I think I am just thinking of the problem wrong
def multiply(*args): retval=0 for num in args: retval *= num return retval
Seth Kroger56,412 Points
I think the "Slices might come in handy" is the key here. Because the type of args doesn't matter the first multiplication of 1 * args may not be sensible if args aren't ints/floats (and it should be 1 to start out with, not 0 because 0 * num is always 0). The trick here is to use the first arg as the initial value and then loop over a slice of the rest.
Philip Schultz11,437 Points
Hey Wayne, I asked how slices would work in this challenge a couple weeks ago. Check out this post where one of the Python Instructors informed us how to use it with slices. https://teamtreehouse.com/community/returning-product