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

Python Question

I need help figuring out what I am doing wrong.

Add a try block before where you turn your arguments into floats.

Then add an except to catch the possible ValueError. Inside the except block, return None.

trial.py
def add(ab1,ab2):
    try:
        return float(ab1) + float(ab2)
except ValueError:
        return ('none')
else:
        return float(ab1) + float(ab2)

1 Answer

Hey there, looks like you need to fix the indentation of the except and else blocks. Also instead of returning 'none' in your except block return None. Here is a helpful stackoverflow question about the None value http://stackoverflow.com/questions/19473185/what-is-a-none-value.

def add(ab1,ab2):
    try:
        return float(ab1) + float(ab2)
    except ValueError:
        return None
    else:
        return float(ab1) + float(ab2)

Thanks for your help!