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.


object orient

Alright, here's a fun task!

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.


2 Answers

def combiner(list1): num = 0 string = ""

for item in list1:
    if isinstance(item, str):
        string += item

    elif isinstance(item, (int, float)):
        num += item

    return string + str(num)

not working

def combiner(my_list):
    string = ""
    sum = 0
    for item in my_list:
        if isinstance(item, str):
            string += item
        elif isinstance(item, (int, float)):
            sum += item

    final_string = string + str(sum)
    return final_string

print(combiner(["apple", 5.2, "dog", 8]))

Please check if this works for you. It did gave out put for me