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

Laknath Gunathilake
Laknath Gunathilake
1,860 Points

cant seem to get this exception to work

I'm not sure if I'm putting the try in the right place. It doesn't seem to work

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

2 Answers

Steven Parker
Steven Parker
231,248 Points

:point_right: You have to indent both the try and the else for them to be considered part of the function.

Also, you must attempt your float conversions inside the try block.

And, if you return inside the try, you won't need an else.

Laknath Gunathilake
Laknath Gunathilake
1,860 Points

I indented both try and else but it doesn't seem to work