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.


Checking for str and int in the isinstance() challenge

So I've used this code (attached) in workspaces and I get the following for each of these tests (which is exactly what the challenge is asking for):

combiner([1, 2, 2, "hello", 5, "all things considered", 10]) = helloall things considered20

combiner(["hi", 1, 200, "hello", 5, "all things considered", 14]) = hihelloall things considered220

What am I doing wrong? Do I need to write an else statement to discard other types?? I just assumed it would iterate over them and not do anything with them.

def combiner(arg):
    my_str = ""
    my_num = 0
    for item in arg:
        if isinstance(item, str):
            my_str += item
        if isinstance(item, int):
            my_num += item
    return my_str + str(my_num)

1 Answer

28,538 Points

Take a second look at the example input and output they provide, there is something about it that is different from the values you have been testing. Namely one of the numbers they pass in is 5.2 which is a float not an int. They do pass in an int as well though.

So your code needs to check for the presence of both floats and ints. Checking for just one or the other is not enough.

Nice thankyou! That worked :)