Welcome to the Treehouse Community
Looking to learn something new?
task 3 try and except
i get error for this code please explain
def add(num1,num2): try: float(num1)=num1 float(num2)=num2 except ValueError: return None else: return (float(num1)+float(num2))
Stuart Wright41,102 Points
Your try block should look something like this:
try: 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.
else: return num1_as_float + num2_as_float