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

Moses Ong
Moses Ong
165 Points

I don't understand how to multiply and use slice in this question

Gosh, stuck at this for a good half an hour.

twoples.py
def multiply(string, *args):
    product = string 
    count = 0 
    for elements in args:
        product += elements

    return product 

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Moses,

Let's work through your code:

def multiply(string, *args):

The challenge is only going to give you numbers, no strings. If you want to specifically separate the first argument from all the subsequent arguments, I'd suggest you give it a more appropriate name, like first_num. Alternatively, you could just take one argument: *args and then access the first element in that in your function body (which would mean you would later be slicing the other elements from *args to work through).

    product = string 

You are only going to be working with numbers, so you wouldn't want to put a string in your product variable and then later combine it with numbers.

    count = 0

Between string and count it's clear that you are looking to have some initial value that is going to be built up every time you cycle through your for loop. However, count never gets used after you initialise it. And product you're initialising with something called string, which implies that you expect this to have a string type instead of a number type.

If you are expecting string to be the first number you are given, then this part will work fine, but you don't want count. If you want to start with a fixed initial value (like we would use 0 if we were repeatedly summing values), you'd want to start with 1, not 0.

Alternatively, if you were just using a single argument, *args and wanted to initialise with the first value from that, you could do:

product = args[0]
    for elements in args:
        product += elements

Your program is supposed to be multiplying all the elements, but you are summing them.

Hope that clears everything up for you

Cheers

Alex