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

My function returns what the question is asking for, why is it not being accepted?

#combiner function takes a list and combines string type and sums integer and float types, returns as one string.
def combiner(lst):
    #empty lists and string that for loops will use.
    str_list = []
    new_combined_str = ""
    num_list = []
    #this loop iterates through lst and pulls out element type string and puts them into str_list.
    for i in lst:
        if isinstance(i, str):
            str_list.append(i)
    #this loop interates through the str_list and concatenates them into one string.      
    for i in str_list:
        new_combined_str += i
    #this loop iterates through the list and pulls out elements type float and puts them into num_list.
    for i in lst:
        if isinstance(i, float):
            num_list.append(i)
    #This loop iterates through the list and pulls out elements type int and puts them into num_list.      
    for i in lst:
        if isinstance(i, int):
            num_list.append(i)
    #sum all elements in num_list      
    num_list = sum(num_list)
    #change num_list to type str and concatenate with new_combined_str
    new_combined_str = new_combined_str+str(num_list)

    return new_combined_str

1 Answer

Steven Parker
Steven Parker
231,269 Points

The "isinstance" method will test more than one type at a time, so you could catch both kinds of number at the same time. You could also simplify things a bit by accumulating both strings and numbers in the same loop.

I don't why this would make a difference, but they did give a warning about being tricky. Beats me how, but if they manage to have something that passes the tests for both types, it would get counted twice using separate loops.