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

Whats wrong with 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(a):
    word_list = []
    number_list = []
    for thing in a:
        if isinstance(thing,str) == True:
            word_list.append(thing) 
        else:
            number_list.append(thing)

    return ''.join(word_list) + str(sum(number_list))

1 Answer

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

don't see anything wrong with your code, actually I tried to paste it to the code challenge and it passed.

thank you for your quick response. Mine doesn't pass for some reason

William Li
William Li
Courses Plus Student 26,868 Points

that's odd. But by looking at your code, correctness is fine, but there's some improvement you can do to simplify the code a little bit.

def combiner(a):
    word_list = ""
    total = 0
    for thing in a:
        if isinstance(thing, str):
            word_list += thing
        else:
            total += thing
    return word_list + str(total)

Try this version and see if it pass the grader.

Thank you very much for your suggestions! Your code passed. I tried my code again after and it also worked.