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

Try and Except Objectives

My first two objectives are passing no problem. However, when it comes to the third objective it fails. Based on previous questions asked about this question it looks like my code is right. It follows the argument, but I am not sure what is failing here. Could someone please shed some light on this? Thanks.

trial.py
def add(arg1, arg2):
  try:
  a = float(arg1)
  b = float(arg2)
  except ValueError:
      return None
  else:
      return(a + b)

2 Answers

Steven Parker
Steven Parker
231,007 Points

With Python, indentation is everything. Your code is fine, but the indentation needs to be fixed. You want the two assignments inside the try, but right now they are between the try and except:

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

The parentheses aren't necessary on the return, but they don't hurt anything.

Okay, I see. I didn't indent the float arguments properly. Thanks for your help!