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

Convert string to float

Hi all,

What wrong with my code ?

Q: Let's make sure we're always working with floats. Convert your arguments to floats before you add them together. You can do this with the float() function.

my wrong Answare:

def add (a, b):
    return float(a + b)

5 Answers

I also tried to convert them before:

def add float(a, b):
    return (a + b)

or

def add (a, b):
    f = float(a)
    g = float(b)
    return (f + g )

It says convert before you add them together, maybe you should first convert them and after add them?

Hi omri,

I presume it will only be numbers entered in which case:

def add(a, b): return float(a) + float(b)

If not they may want the try, except route before returning.

This shoud work. What I did here, is just floated our argumnets befor I returned them.

def add(num1, num2):
    a =  float(num1)
    b = float(num2)
    return a + b

Hope this helped :)