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!

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

task 3 try and except

i get error for this code please explain

def add(num1,num2):

    except ValueError:
        return None
        return (float(num1)+float(num2))

1 Answer

Stuart Wright
Stuart Wright
41,118 Points

Your try block should look something like this:

    num1_as_float = float(num1)
    num2_as_float = float(num2)

Your current version is not allowed because you are trying to assign to something which is not a variable name - "float(num1)". A variable name must always be on the left of an assignment statement, and a function such as float() can only be on the right.

Your except block is correct.

The else block is almost correct, except you just need to return the sum of your two new variables - they will already be converted to floats if your try block is correct, so there's no need to convert to float again.

    return num1_as_float + num2_as_float

Thank you.