Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Tuples Packing

sarvienn thevendran
sarvienn thevendran
734 Points

I'm not so sure how to use unpacking and to get this done, some help would really be appreciated, thank you!

it returns an error saying int is not subscribable

twoples.py
def multiply(*args):
    pos = 0
    total = 0
    for number in *args:
        total += num[pos] * num[pos + 1]
        return total

2 Answers

John Lack-Wilson
John Lack-Wilson
8,169 Points

Hi Sarvienn, thanks for your question.

The way you've approached it may be slightly over-complicating things. Here are the steps I took to complete the challenge:

  • Define the multiply function that takes *args as a parameter
  • Make a total variable and assign it the value of 1 (we cannot use 0, since 0 * anything = 0)
  • Use a for loop to iterate over the *args like: for arg in args
  • Use the '*=' operator to multiply the new arg with the previous one and assign it to total
  • Return total (outside of the for loop)
sarvienn thevendran
sarvienn thevendran
734 Points

Hey John! first of all thanks for responding!

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

i typed out the code like this upon your response, however, it still does not work! would you be so kind to analyse this and point out the flaws?

thank you!

John Lack-Wilson
John Lack-Wilson
8,169 Points

It's hard to say exactly why because your comment is not formatted like normal code (python takes indents seriously!)

Here's what it should look like based on your code:

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

Note the return statement is outside the for loop. If the return statement is inside the for loop then it will return after one iteration of the loop.

sarvienn thevendran
sarvienn thevendran
734 Points

thank you, John! it appears to have been the indent..

turaboy holmirzaev
turaboy holmirzaev
17,716 Points
          If you are only unpacking, then you want to access the individual members of the collection passed into your function. 
          So, once you define your function as
            def my_func(*args): => means that the parameter "*args" that is being passed is some collection like lists, tuples, or dictionaries and in the body I want to something on individual elements of it one by one. 
    if that is the case, when calling your function, don't forget to add (*) before the argument 
     EX:
           def my_func(*args):
             total = 0
             for item in args: 
                 total  += item 
             return total 
Calling: 
>>>numbers = [1,  2, 3, 4] 
>>>my_func(*numbers)
>>> 10

Just do not forget to add * when calling your function 
And the way you organized code you may have IndexError because you are one step ahead by doing 
pos + 1 in one line
          ```