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

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

Slices issue

Where does slices come in this challenge? I dont know what to do with slices here and the code gives the try again error.

twoples.py
multiply(*nums):
    total = 0
    for num in nums:
        total * num
        return total

2 Answers

Steven Parker
Steven Parker
231,236 Points

You don't need to use slices.

What you were doing will work just fine, but you have to fix a few things yet:

  • a function definition must start with the word "def"
  • when you are multiplying, you must start with 1 — anything multiplied by 0 is still 0
  • to both multiply and reassign the value, the operator is "*="
  • check the indentation, the return should happen after the loop

I was typing the same thing, then got busy with something else and came back to your answer. :)

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

These kinds of errors are really frustrating. All I was thinking of was to initialise the variable with zero, not taking the multiplication into account. Thanks alot Steven.

Mischa Yartsev
Mischa Yartsev
20,562 Points

Hey, guys!

But it is still very unclear why Kenneth mentioned slices in this challenge. Is someone good enough to explain it? Maybe Kenneth Love?

Thanks.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Slices can save you a step. You have to multiply all of the values in *args, right? And you can't start multiplying by 0. So you set a default value of 1...but why? You already have the first number in *args, so use that and then loop through args[1:].

Kenneth Love I'm picking up what you're throwing down. I was racking my brain about the slice thing too. I was thinking there was a solution using slices that used less code. Correct me if I'm wrong, but using this slice method doesn't solve in less code it is just taking out a step of using a variable with a 1.

Kenneth Love when I was trying to figure out what that line meant in the question I was trying to make it harder than it needed to be. Once again, your ninja abilities have impressed me.

Steven Parker
Steven Parker
231,236 Points

Using the slice potentially saves a step in the loop.

But there's an even more elegant one-line solution possible using techniques that will be introduced in later courses.