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

Luis Munguia Salas
Luis Munguia Salas
14,797 Points

I've tried the except BEFORE the arguments are turned into floats, like the instructions say, in various forms

Any know how they answered the 3rd coding exams where, you have to enter the except before the arguments are turned into floats???

trial.py
def add(arg1, arg2):
    new_arg1 = float(arg1)
    new_arg2 = float(arg2)
    total = new_arg1 + new_arg2
    return total

1 Answer

Hey Luis,

I'm not seeing the try/except/else structure within your above code. Per the example below, you'll see that below my function declaration, I indent once and set-up my try block. This is where you'll want to convert the numbers passed into floats. You'll want to follow this block with a ValueError exception, just in case a non-numeric value is passed to the function. Finally, in the else block, you'll return the two values added together.

def add(x, y):
    try: 
        foo1 = float(x)
        foo2 = float(y)
    except ValueError:
        return None
    else:
        return foo1 + foo2