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

need help

def add(x, y): # number 1 and 2 return x + y # number 3

def add(num1, num2): try: a = float(num1) # <-- indent b = float(num2) # <-- indent except ValueError: return None else: return(a + b)

def add(num1, num2): try: a = float(num1) # <-- indent b = float(num2) # <-- indent except ValueError: return None else: return(a + b)

I need help the third part does not work and have been dealing with this for about a week

trial.py
def add(x, y):    # number 1 and 2
    return x + y   # number 3

def add(num1, num2):
  try:
      a = float(num1)  # <-- indent
      b = float(num2)  # <-- indent
  except ValueError:
      return None
  else:
      return(a + b)

  def add(num1, num2):
  try:
      a = float(num1)  # <-- indent
      b = float(num2)  # <-- indent
  except ValueError:
      return None
  else:
      return(a + b)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Looks like the last def statement is indented too far.

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

Your code for part 3 looks good. I just tried it and it passed. Make sure you don't have all the code you posted above in the trial.py file. You should have only this:

def add(num1, num2):
  try:
      a = float(num1)  # <-- indent
      b = float(num2)  # <-- indent
  except ValueError:
      return None
  else:
      return(a + b)