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

Robert Baucus
Robert Baucus
3,400 Points

Combiner.py (OO Python), can't figure why it's adding my numbers as strings...

So I got the program to work in my IDE (sort of) but it keeps giving me the numbers added together as a string (i.e. concatenated instead of combined in to a sum). There is something super basic that is wrong here but I can't see the forest for the trees.

Any thoughts Innernet friends?

def combiner(list):
    number_output = 0
    output = []
    for i in list:
        if isinstance(i, str):
            output.append(i)
        elif isinstance(i, int) or isinstance(i, float):
            number_output += i
    return "".join(output) + str(number_output)


print(combiner(['dog', 'apple' '5', '4.5']))

# yields: dogapple54.50
instances.py

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The combiner is only testing if the instance is an actual int before adding them. There is not conversion to an int or float. Your test data contains no integers. Try calling the code with `print(combiner(['dog', 'apple' 5, 4.5])).

Post back if you need more help. Good Luck!!

Robert Baucus
Robert Baucus
3,400 Points

I completely missed that I had put '5' in the arguments, thanks!