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!

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

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.

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

1 Answer

Steven Parker
Steven Parker
228,131 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