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 trialJustin Hein
14,811 PointsWhy "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:
- The input isn't being properly unpackaged and therefore, a
for
orwhile
loop is irrelevant - The argument cannot simply be multiplied because it's not an
int
- 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
231,236 PointsYou'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.
Justin Hein
14,811 PointsJustin Hein
14,811 PointsThank you Steven! I knew it was something small! In the end, I ended up with:
Steven Parker
231,236 PointsSteven Parker
231,236 PointsEven better! Good job!