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

Why "might" slicing come in handy?

Alright, I'm sure I've made this more complicated than it should be, but packaging/unpacking it confusing to me, so hang tight. Prior to this version of code, I attempted something like:

for num in args:
      total *= num

But that didn't seem to go over well. I'm not quite sure where the breakdown is occurring. If I could see the console output I think I'd know, but I am unsure. Here is what I think:

  1. The input isn't being properly unpackaged and therefore, a for or while loop is irrelevant
  2. The argument cannot simply be multiplied because it's not an int
  3. I've completely missed the point and there's something else

Would love some thoughts on where I went wrong here.

twoples.py
def multiply(*args)
    total = 1
    i = 0    
    while i <= len(args):
        total *= args[i]
        i += 1
    return total

1 Answer

Steven Parker
Steven Parker
218,695 Points

You're really close here! Just two issues:

  • the "def" line needs a colon at the end
  • the highest index is less than the length, instead of "<=" use "<"

And for this strategy, you didn't need a slice. The hint would apply to a different strategy where you started with the first item (instead of 1) and then multiplied by the items in a slice with everything else in it.

Thank you Steven! I knew it was something small! In the end, I ended up with:

def multiply(*args):
    total = 1  
    for num in args:
      total *= num
    return total