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

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.

instances.py
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

andren
andren
28,558 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 :)