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


Shawn McCall
Shawn McCall
7,863 Points

OOP Python Code Challenge : Using isinstance() function getting a TypeError. Code with sample arg works in Workspaces.

With the sample argument ["apple", 5.2, "dog", 8] provided, I am getting the expected output "applesdog13.2" in workspaces, but in the challenge its giving me a TypeError. What am I missing?

def combiner(arg):
    arg_sum = 0
    arg_string = ""
    for item in arg:
        if (isinstance(item, (int, float))):
            arg_sum += item
    arg_string = "".join(arg)
    return arg_string + str(arg_sum)

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Shawn,

You have to be very careful when removing an item from a list while you are iterating through it. Your code works fine for the example given, but if you test your code with a list where an integer occurs twice in a row, you'll get the error you are experiencing.

This is because as an item is removed during iteration, you can end up skipping the next item. If that next item happens to be an int, then you will end up trying to join a list of integers and strings, which will throw a TypeError.

Shawn McCall
Shawn McCall
7,863 Points

Ah, I see now. Thank you.