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

What's wrong in this code?

I can't figure it out what's wrong in this code.

I'd appreciate your help.

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

the else might be redundant, but the challenge asks for it. Although it's interesting how Samuel made it work without one. Bringing the final return out to the same indent as try and except just makes it the next thing to happen. Fun stuff. :D

1 Answer

Samuel Ferree
Samuel Ferree
31,722 Points

Python uses what's known as "significant whitespace." The indention isn't just there to make your code look pretty, it as a functional purpose.

You just need to indent the lines of code after your try statement, like so. (The else is also a bit redundant, so I've removed it)

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

Yes. I fixed the indention. Thanks for your help.