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

Moses Lagoon
PLUS
Moses Lagoon
Courses Plus Student 660 Points

Doing this for try catch of the exceptions and it's not working.

def add(x,y): try: nx = float(x) ny = float(y) except valueError: return None else: return(nx + ny)

trial.py
def add(x, y):
    try:
        nx = float(x)
        ny = float(y)
    except ValueError: 
        return None
    else: 
        return (nx + ny)

2 Answers

There is multiple ways that you can write this and I'm almost 100 percent sure someone will gig me for not showing you a quicker way to write this lol but I think this is a better way to show beginners as it seems a little easier to comprehend what is going on, with all that said here is the code:

def add(arg1, arg2):

    try:

        arg_float1 = float(arg1)
        arg_float2 = float(arg2)

    except ValueError:
        return None
    else:

        return arg_float1 + arg_float2

As I now look at your code closely you just need to remove your parenthesis lol.