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

Rasheed Ajala
PLUS
Rasheed Ajala
Courses Plus Student 2,003 Points

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.

28 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

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!

Rasheed Ajala
Rasheed Ajala
Courses Plus Student 2,003 Points

Thanks it worked. But do you know why mine didn't? keeping in my mind that I added all parentheses.

Ryan Ruscett
Ryan Ruscett
23,309 Points

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?

Rasheed Ajala
Rasheed Ajala
Courses Plus Student 2,003 Points

Ohh right. Yeah that solves my problem. Didn't understand the question enough. lol, thanks man.

Ryan Ruscett
Ryan Ruscett
23,309 Points

Awesome! Good Luck pythonista!

Gary Gibson
Gary Gibson
5,011 Points

This only worked for me when I kept a as a and b as b instead of turning them into x and y...

a = float(a) b = float(b)

Zeljko Porobija
Zeljko Porobija
11,491 Points

Is else: intended properly? It seems to me it's not, you added an extra space.

Megan Riley
Megan Riley
5,150 Points

Any reason why this wouldn't work for step 3?

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

Paul Je
Paul Je
4,435 Points

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!

Vedran Linić
PLUS
Vedran Linić
Courses Plus Student 18,802 Points

Fore final challange this is the code that i used

def add(num1, num2): try: return float(num1) + float(num2) except ValueError: return None else: print(num1 + num2)

paul chon
paul chon
1,605 Points

assign the process of adding the two floats into a variable.

def add(x, y):
    try:        
        a = float(x)
        b = float(y)
        total = a + b
    except ValueError:
        return None
    else:
        return total
Patric Daniel Pförtner
Patric Daniel Pförtner
1,542 Points

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 :)

Thomas Souza
Thomas Souza
7,943 Points

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

Patrick Chumley
Patrick Chumley
6,009 Points

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!

Rasheed Ajala
Rasheed Ajala
Courses Plus Student 2,003 Points

I added the parenthesis in my original code, that was an omission. Thanks for catching that

Petrius Balbinot
Petrius Balbinot
1,805 Points

None of the codes posted above work, correct?

You will want to modify the else statement.

else:
    print(a + b)
Aaron Mckenzie-Hunte
Aaron Mckenzie-Hunte
10,741 Points

Hi,

If you are still stuck, try this:

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

This is the solution that allowed me to pass Q 3/3

Peter Rzepka
Peter Rzepka
4,118 Points

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

Kong Cheong
Kong Cheong
633 Points

Hi, I followed the steps but unable to get it to work. I completed 1/3 and 2/3 perfectly then 3/3 is returning error every tiime I submit. Please help, I have no idea what is wrong.

Kong Cheong
Kong Cheong
633 Points

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
Zeljko Porobija
Zeljko Porobija
11,491 Points

else should only have the return statement. No other statements are needed at all.

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

def add(x, y):

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

else: return x + y

Ivan K
Ivan K
1,928 Points

helle everybody!

do you know, why my code doesn't work?:

def add(one, two): try:
o = float(one) t = float(two) except ValueError: return None else:
return (o+t)

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))
Paul Je
Paul Je
4,435 Points

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)

Jarrod Gillis
Jarrod Gillis
16,760 Points

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)

Bo Winters
seal-mask
.a{fill-rule:evenodd;}techdegree
Bo Winters
Full Stack JavaScript Techdegree Student 1,481 Points

def add(a, b): try: a = float(a) b = float(b) except ValueError: return None else: return(float(a + b))
After trying everything else, this one finally worked for me!

Aron Katz
Aron Katz
700 Points

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

ANDY VAN DEN BOS
ANDY VAN DEN BOS
2,330 Points

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!

reza rahnama
seal-mask
.a{fill-rule:evenodd;}techdegree
reza rahnama
Python Web Development Techdegree Student 1,118 Points

def add (exe1, exe2): try: a = float (exe1) b = float (exe2) total = a + b

except ValueError:
    return None
else:
    return total

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.

Rehan Hussain
PLUS
Rehan Hussain
Courses Plus Student 2,199 Points

Gusy - Why isn't this code running this is so frustrating! They need to have a live chat or something, such a waste of time!

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

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

B W
B W
2,523 Points

def add(dance, play): try: a = float(dance) b = float(play) except ValueError: return None else: total = a + b return total

Jason Turla
Jason Turla
2,575 Points

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.

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