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.

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
218,487 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,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[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,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

wayne ross
wayne ross
3,225 Points

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