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

Martin Bornman
PLUS
Martin Bornman
Courses Plus Student 12,662 Points

Try

In my try part of the last challenge I got stuck.Dont know if my syntax is'nt correct.Can you perhaps help me out?

trial.py
def add(z, y):
try:  
  return(float(z) + float(y))
except ValueError:
  return(None)
else:
  add(4, 6)

1 Answer

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey Martin,

there are a few mistakes in your code. Firstly you have to indent everything inside of your add method. Secondly the challenge just wants you to change the arguments to floats in the try block without returning the result. You should however return them in the else block.

Please also note that return is no method so you don't have to write parentheses around the value you return and that you can't call the method in the method itself like you do it in your else block.

It should work like this, if you have further questions feel free to ask! :)

def add(arg1, arg2):
  try:
    arg1 = float(arg1)
    arg2 = float(arg2)
  except ValueError:
    return None
  else: 
    return arg1 + arg2