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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

Unclear about the task

I am embarrassed to admit this but I can't figure out what the task is requesting, especially with the hint about using slices. It says nothing about how to use a tuple. I can't figure out how to set it up for a product. Maybe someone can give me a hint?

It looks something like what Prof Love does with the add function about mid-way through the preceding video...this one.

def add(*nums):
       total = 0
       for num in nums:
             total += nums
      return total 

I see the pattern but I don't see the use of tuples, or how to integrate slices. I am hoping not to hit a wall here. The fact that I am clueless about this is not making me optimistic.

twoples.py

Nancy Melucci
Nancy Melucci
Courses Plus Student 36,143 Points

Here's the challenge...I don't know if you can see it, and I had no code because I am stumped.

"Let's play with the *args pattern. Create a function named multiply that takes any number of arguments. Return the product (multiplied value) of all of the supplied arguments. The type of argument shouldn't matter. Slices might come in handy for this one."

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hint #1: No, you do NOT need to use slice to solve this challenge, and I personally think there's no point in using slice here.

Hint #2: In the add function, the use of tuple is here.

       for num in nums:
             total += nums

it loops through each item in the nums tuple, and add 'em to the total

Hint #3. the multiply function is extremely similar to the add function. But it's important to set the total's initial value to 1 instead of 0, otherwise total would always be equals to 0 no matter how many multiplications are performed.

hope it helps.

Thanks for that William. Good hints without giving the answer.

Simin Hajizadeh
Simin Hajizadeh
3,072 Points

I wrote the program as below:

def multiply(*args): total = 1 for item in args: total = total *item return total

Can someone explain this code challenge and its purpose ? the more I continue the more i get lost. Really hard for me right now