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

Nico Romero
Nico Romero
1,365 Points

Exceptions with floats and except ValueError, etc

Im having a hard time formatting my code correctly. It is asking that I use the try, except and else for exceptions, I have formatted them like the videos but I am still getting a try again. Any suggestions? Thank you!

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

1 Answer

Stuart Wright
Stuart Wright
41,119 Points

Your code has two errors:

1 - typo 'retun' inside your except block.

2 - the entire body of the function needs to be indented.

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