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

Sara Watson
PLUS
Sara Watson
Courses Plus Student 3,713 Points

How would you use slices in this solution?

The challenge stated that slices might come in handy for this solution. Does anyone have a solution for this challenge that uses slices? I don't understand how to do that in the context of this challenge.

This is my working multiply function, without using slices:

def multiply(*args): product = None for arg in args: if product: product *= arg else: product = arg
return product

1 Answer

Elad Ohana
Elad Ohana
24,456 Points

Hi Sara,

Here is my solution:

def multiply(*args):
    product = args[0] .  # starts with the first item
    for arg in args[1:]:    # skip the first item for multiplications
        product *= arg
    return product

This reduces the need to compare the 'product' variable on each iteration. Hope this helps!

Elad

Sara Watson
Sara Watson
Courses Plus Student 3,713 Points

Wow, I understand now. That makes total sense. Thank you!