Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
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,406 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,413 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