Welcome to the Treehouse Community
Looking to learn something new?
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!
def add(a, b): float(a) float(b) c= a + b return c
Lok C10,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)