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 trialVanessa Van Gilder
7,034 PointsIs this how you would use slices in the twoples challenge?
Okay, so I realize that you don't need to use slices to pass this challenge (I've already done it two other ways without slices and passed) but I'm trying to figure out if this is what it should be if you do use them. On another question Kenneth Love wrote, "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:]." I don't see how this is saving a step so is this not what he meant?
def multiply(*nums):
total = nums[0]
for num in nums[1:]:
total *= num
return total
I might have just figured out the answer right after typing all this up (based on another commenter) but is it that it doesn't save a step in the code but rather the computer makes one less loop?
2 Answers
Steven Parker
231,275 PointsThat's exactly right, using slices allows you loop one less time. The "step" mentioned is a "loop step" or iteration.
Ibrahim Khan
3,353 PointsI passed the coding challenge after a few trial and errors but I was curios to getting it working in the workspaces.
Lets say you are working on workspaces and use Vanessa's function. you want the user to be able to input the values and get the result by running python mutiply.py. Lets say a = input("> "), and the input is > 2, 4, 5. How would you pass this input into the code to get 40 as the answer...
Steven Parker
231,275 PointsYou'd need to parse the string to separate the three values, and then convert them from strings into numbers. Finally, you could pass the numbers to the "multiply" function.