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

Didier Borel
Didier Borel
2,837 Points

what is wrong with my try, else

i don't understand what I am doing incorrectly here

trial.py
  def add(var1,var2)
  try:
    return(var1+var2)
  except:valueError
    return None
  else:
    return (float(var1)+float(var2))

2 Answers

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey Didier,

Michael already mentioned one mistake. Besides this one also note that you need a colon after the line where you declare your function and take care that you indent correctly. Then also note that you have to change the function's arguments to floats in the try block to catch a possible error when changing the arguments to floats instead of returning the result in the try block which would stop the function's execution.

It should look like this then:

def add(var1,var2):
  try:
    var1 = float(var1)
    var2 = float(var2)
  except ValueError:
    return None
  else:
    return var1 + var2

I hope that helps! :)

Michael Hulet
Michael Hulet
47,912 Points

The line where you try to catch a ValueError has a couple syntax errors. First of all ValueError is capitalized. After that, the colon on that line needs to go at the very end, like this:

except ValueError: