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

Carole Cox
Carole Cox
429 Points

looks good but its not...

Not sure about the final return, should it be return (num) with an num=num1+num2 or does return num1+num2 handle that? ORRRRR should it be return (num1+num2). Why's and why's not? Thank you!

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

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi there.

Your code is fine, what prevents it from passing is the indentation, you see, indentation is a big part of Python's syntax.

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

Shift the indentation of all lines of your code after line 1 one level to the right, and now it should be fine.

Carole Cox
Carole Cox
429 Points

Thanks William!

Yes, once I got it all lined up...it worked. Took me a sec. NEWBIE MISTAKES! Its getting better...