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 Python Basics (2015) Logic in Python Try and Except

Holden Glass
Holden Glass
6,077 Points

Try and except

On step 2, I turn the arguments into floats but I keep getting "Bummer! 'add' returned the wrong number." I tried it out in workspaces and it worked. Why wouldn't it work here?

trial.py
def add(n1, n2):
    float(n1)
    float(n2)
    return (n1 + n2)

2 Answers

If you just call the float function it won't actually change the variable.

That is important. You must turn them into floats and then return them directly (after adding) like this:

def add(num1, num2):
    return float(num1) + float(num2)

Good luck! ~Alex

Ryan S
Ryan S
27,276 Points

Hi Holden,

The issue is that although you ran your arguments through the float() functions, you are still returning the sum of the original arguments. They haven't been permanently converted into floats. There are a few ways to do this, but you could either re-assign the variables, i.e., n1 = float(n1) and then leave your return statement as is. Or you could convert each argument in the return statement itself and add them together.

Good Luck,