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

Dillon Reyna
Dillon Reyna
9,531 Points

I Don't Understand the Question

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

I don't get where to insert the 'try:' statement, so I keep getting this wrong.

trial.py
def add(a, b):
    return (float(a) + float(b))

try:
    add(1, 2)
except ValueError:
    return None
else:
    add(1, 2)

1 Answer

Aaron Nolan
Aaron Nolan
5,714 Points

You're very close with your answer but Treehouse wants you to add the try/catch block into the method, so your code should read like this :

def add(num1, num2):
    try: # Try turn these values into floats
        return float(num1) + float(num2)
    except ValueError: # If it can't, don't return anything
        return None
    else: # If it can then continue returning them
        return float(num1) + float(num2)

Hope this helps! Happy Coding :)