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 trialAnthony Ruvinov
Courses Plus Student 7,638 PointsCreate a function named multiply that takes any number of arguments. Return the product (multiplied value) of all of the
Can someone please help me I am really lost on this.
2 Answers
Philip Schultz
11,437 PointsHey, It is asking you to make a function that accepts an *args argument. Then it wants you to loop through it and multiply all of the values.
def multiply(*args):
product = 1
for item in args:
product *= item
return product
THAPELO MOKOKO
9,457 PointsWrite a function, named multiply, that takes a value as an argument, then returns that value multiplied by 2.
help plz
Anthony Ruvinov
Courses Plus Student 7,638 PointsAnthony Ruvinov
Courses Plus Student 7,638 PointsThanks, I just get stuck a lot and i dont know what to do. This helps me understand it more but i dont understand why you do a for item in args.
Philip Schultz
11,437 PointsPhilip Schultz
11,437 PointsThe *args is allowing you to accept an arbitrary amount of arguments into the function. Meaning all of these would be acceptable when calling the function.
*args takes all of the arguments, like (1,2,3) and packs them into args. You then have to loop through the args to access each value within the function. Does this help?
Philip Schultz
11,437 PointsPhilip Schultz
11,437 PointsThis video makes it pretty clear to me. See if this helps https://www.youtube.com/watch?v=762mFeD2SlU
Phillip Bailey
1,822 PointsPhillip Bailey
1,822 PointsI like what I see, but I'm confused. Seems like this is doing too much work? How come you can't just say return base *= args
how would a beginner know to use a for loop over this tuple? I understand for each of the args in the tuple, multiply 1* that arg, but I'm confused on how it knows to multiply the total as opposed to just printing 1* arg in a row.