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.

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!!!! ;)