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!
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
sarvienn thevendran734 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
def multiply(*args): pos = 0 total = 0 for number in *args: total += num[pos] * num[pos + 1] return total
John Lack-Wilson8,181 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)
turaboy holmirzaev19,197 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 ```