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 trial

Python Python Collections (2016, retired 2019) Tuples Packing

Anthony Ruvinov
PLUS
Anthony Ruvinov
Courses Plus Student 7,638 Points

Create 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.

twoples.py

2 Answers

Philip Schultz
Philip Schultz
11,437 Points

Hey, 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
Anthony Ruvinov
Anthony Ruvinov
Courses Plus Student 7,638 Points

Thanks, 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
Philip Schultz
11,437 Points

The *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.

multiply(1)
multiply(1,2)
multiply(1,2,3)

*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
Philip Schultz
11,437 Points

This video makes it pretty clear to me. See if this helps https://www.youtube.com/watch?v=762mFeD2SlU

I 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.

Write a function, named multiply, that takes a value as an argument, then returns that value multiplied by 2.

help plz