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

Python returning error!

The question is " Convert the arguments into floats by using the function ' float()' and then add the two arguments and return the total " Please help me to edit this code!

trial.py
def add(a, b):
    float(a)
    float(b)
    c= a + b
    return c

1 Answer

Lok C
Lok C
10,617 Points

That's because while float(a) returns the float version of a, it doesn't assign the new value for the name a. So when you run

c = a + b

a and b are referring to the same thing as before.

To convert a to float and use it later for c, you have to tell python which name to point to the new value. So for instance to use the float version of a to calculate the sum you will need to do something like

a_float = float(a)

You can also do more directly

c = float(a) + float(b)

and then return c. Or even more straight-forward, just return the value without bothering with assigning new names:

return float(a) + float(b)

Thank you so much!!!! ;)