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 trial

Python Python Collections (2016, retired 2019) Tuples Packing

wayne ross
wayne ross
3,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

twoples.py
def multiply(*args):
    retval=0
    for num in args:
        retval *= num
    return retval
Steven Parker
Steven Parker
231,275 Points

:mailbox_with_mail: Hi, I got your request, but it looks like the other answers have already covered your question.
Happy coding!

3 Answers

Seth Kroger
Seth Kroger
56,414 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[0] 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 Schultz
Philip Schultz
11,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

wayne ross
wayne ross
3,225 Points

Thanks as usual I just wasnt considering the inputs. on to Tuples!!!