## 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!

### 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.  PLUS

# how do i convert an argument in my function to float. This is python.

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.

The first question says to create a function add that takes two arguments and returns their sum. The second question is above.

This is what I did and I think should be correct: def add(x,y): return(float(x +y)

It worked on my python shell on my computer but i keep getting a wrong feedback in the quiz. Hola,

So the question says

create a function add the two arguments and returns their sum

```def add(x , y):
return x + y
```

At this point we assume we are just adding numbers together.

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.

Ok let's make sure no matter what comes in it's converted to a float

```def add(x , y):
a = float(x)
b = float(y)
return a + b
```

Ok now I converted each value to a float. It could be any whole number or float passed in, and no matter what it is, I am going to turn it into a float.

BUT WAIT? What happens if I pass in a string? That can't be converted to a float. When a string like "hello" is converted into a float, it fails. It gives you a ValueError. Meaning the value can't be converted to float. can't convert a text string into a float. So we catch that and return nothing. Otherwise we were able to convert them both successfully and return true.

This might be the 3/3 in the challenge I think.

```def add(x, y)
try:
a = float(x)
b = float(y)
except ValueError:
return None
else:
return True
```

Let me know if this makes sense or not. If not I can attempt to approach it another way.

Thanks! Yup I do.

```def add(x,y):
return(float(x + y)
```

First thing is first.

1. You need a space between return and the first (.
2. If you write (float(x + y) <---- you forgot to close a parentheses A: return (float(x + y)) <--- need TWO

3. This is how what you are saying is being interpreted.

```return(float(x + y)
```

Say I am Python....

first I use order of operation in a sense. I will do the first bit of code in () Which is x = y. Ok cool I did that. Now I move outward to the second set (float( ) Which is float. So now I convert the value of x + y to a float. Then I return a float.

BUT WAIT!!!

I don't want to return a float. i want to make sure the two values I am adding are floats and if they are not floats, than I need to fail.

Does that help? if you copy and paste this code, it doesn't work, is it because you need to add a return statement below the returned True statement? I tried that as well but maybe the order in which you place it either in the try block or else block may matter or have some different unique syntax attached to it..

I did this and its saying "Oops! Looks like Task 1 is no longer passing."

What am I doing wrong?

hi, this work for me!

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

Thanks! This worked.

Thanks! Hi Rasheed,

You are very close to the answer, here is how I did it:

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

Why are you learning Python? Maybe we have the same aims! I am learning it to bring my Start UP www.wolf-gate.com to the next level. Thank´s to Kenneth Love it´s easily possible :) Your problem is having to do with your syntax. Your return needs two sets of parentheses like this:

def add(x, y): return(float(x + y))

That should fix the issue you are encountering!

You will want to modify the else statement.

```else:
print(a + b)
``` Hi Rasheed,

Thank you for reaching out. I posted a discussion and they helped me figured it out. So I am good to go. I believe my code were correct and there is some spacing/indent issues I have.

I appreciate it a lot for replying! Happy Holidays!

What am I doing wrong here?

```def add(x, y)
try:
a = float(x)
b = float(y)
except ValueError:
return None
else:
return True
a = float(x)
b = float(y)
return a + b
```

Do not know if you are still monitoring this thread, however, you are missing a colon after...

...and then clean up your else statement (indents where appropriate of course)...

else: return x + y

How is a float a letter?

had try different variant without luck what i am missing???

```def add(hola,hello):
try
a = float(hola)
b = float(hello)
except ValueError:
return None
else:
return(float(a + b))
``` Is there a complete answer? Haha I tried mine using some of the tips on here and still can't figure it out.. my code looks like this:

def add(x, y): try: a = float(x) b = float(y) except ValueError: return None

else: return True return(a + b) BASIC CONFIGURATION:

def add(points, rebounds): points = float(points) rebounds = float(rebounds)

```    return(points + rebounds)
```

WITH TRY AND EXCEPT:

def add(points, rebounds): try: points = float(points) rebounds = float(rebounds) except ValueError: return None else:
return(points + rebounds) I am getting no where fast on this last challenge too, it's been wasting a lot of my time and makes no sense, I have just about had enough.

def add(num1, num2): try: a = float(num1)
b = float(num2) except ValueError: return None else: return (float(a + b)) the answer: def add(num1, num2): try: a = float(num1) b = float(num2) except ValueError: return None else: return a + b

Now keep in mind "try, except, and else" must all be in line with each other as in- properly indented.

Hope someone finds this sooner then It took me! def add (exe1, exe2): try: a = float (exe1) b = float (exe2) total = a + b

```except ValueError:
return None
else:
```

I have tried various combinations and not able to get this to work, Can some one advise whats wrong with the above syntax as logically I can't figure it out why it wouldn't work

Make sure all of the indentations are correct! I input all of these codes and the only thing incorrect was improper indentation. So if you're stuck and you know your syntax is correct, make sure the indents are proper! Thanks!

Yes, for me the prob was the indentation! So even with a correct code, it will throw an error if you are not watchful of the spacings.

The following worked for me:

def add(num1, num2): try: num1F = float(num1) num2F = float(num2) except ValueError: return None else: total = num1F + num2F return total This worked for me. What I seem to have done differently than what others have shown is making 'total' a variable and return the variable. In the first part total = a + b then it needs to be changed since they were changed to a float.