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
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:
- The input isn't being properly unpackaged and therefore, a
whileloop is irrelevant
- The argument cannot simply be multiplied because it's not an
- 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
Steven Parker228,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.