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.

Python

Need help with Combiner - OOP

Im not sure what im doing wrong here, testing for Int, str, and float. Any help would be appreciated!

instances.py
def combiner(string):
    num = 0
    string1 = []

    for x  in string:
        if x isinstance(x, int):
            num  += x 
        elif x isinstance(x , float):
            num += x
        else:
            if x isinstance(x, str):
                string1.append(x)

    return "{}{}".format(str(num), string1)

1 Answer

Rich Zimmerman
Rich Zimmerman
24,063 Points

The isinstance method returns a boolean so you don't need to have it say

if x isinstance(x, int):

You only need

if isinstance(x, int):

You also have a mistake in your Else: part of the statement.

else:
            if x isinstance(x, str):
                string1.append(x)

You don't need the if, you could add an extra Elif to test for string and then continue in the 'Else' should a datatype be given that is not an int, str, or float.