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 trialAbdulkadir Kollere
1,723 PointsSlices 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.
multiply(*nums):
total = 0
for num in nums:
total * num
return total
2 Answers
Steven Parker
231,269 PointsYou 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
Mischa Yartsev
20,562 PointsHey, 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
Treehouse Guest TeacherSlices 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:]
.
Dustin James
11,364 PointsKenneth 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
Treehouse Guest TeacherDustin James Yep, like I said, saves a step.
Dustin James
11,364 PointsKenneth 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
231,269 PointsUsing 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.
Dustin James
11,364 PointsDustin James
11,364 PointsI was typing the same thing, then got busy with something else and came back to your answer. :)
Abdulkadir Kollere
1,723 PointsAbdulkadir Kollere
1,723 PointsThese 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.