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

Spencer Hurrle
Spencer Hurrle
3,128 Points

Object-Oriented Python: isinstance Challenge

I have no idea why this approach isn't working. I ran the code in a workspace and it worked just fine with the demo "appledog13.2" list. When I try to run it in the challenge, I get a Bummer that says the += operator can't be used for 'int' and 'list'. Where am I going wrong? I've included the challenge parameters and my code.

'Create a function named combiner that takes a single argument, which will be a list made up of strings and numbers. Return a single string that is a combination of all of the strings in the list and then the sum of all of the numbers. For example, with the input ["apple", 5.2, "dog", 8], combiner would return "appledog13.2". Be sure to use isinstance to solve this as I might try to trick you.'

instances.py
def combiner(*args):
    nums = 0
    words = ''
    for item in args:
        if isinstance(item, str):
            words += item
        else:
            nums += item
    return words + str(nums)

I've edited my code after looking into other peoples' solutions. I'm assuming Kenneth's trick was including a list inside the list...?

instances.py
def combiner(*args):
    nums = 0
    words = ''
    for item in args:
        if isinstance(item, (int, float)):
            nums += item
        elif isinstance(item, str):
            words += item
    return words + str(nums)

Now I'm getting a message that says only "Did not get the expected output".

1 Answer

Steven Parker
Steven Parker
231,236 Points

You're really close, but the instructions say the function "takes a single argument, which will be a list". And the "splat" operator ("*") is only used when a function takes a quantity of individual arguments which you need to have converted into a list.

Spencer Hurrle
Spencer Hurrle
3,128 Points

Thanks Steven! I know this is probably the 4th or 5th time you've answered this question lol I saw your help all over everybody else's struggle with this problem. I thought the *args was for taking in a list, but the way you explained it makes sense to me. I'll give it another go!