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

Try and Except block syntax errors?


I have entered the following code below for this problem. I think something is wrong with my syntax, but I am not sure what. Did I use the try and except functions correctly?

def add(sex, age): try:
return(float(sex)+float(age)) except ValueError: return(None)

def add(sex, age):
except ValueError:

2 Answers

Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey Jacob,

you have to indent everything inside your add function and also note that return is no method so you don't have to use parentheses for your return statement.

About the challenge: The challenge wants you to change your parameters to floats in the try block and then return the result in the else block.

So your code should end up like this:

def add(sex, age):
    sex = float(sex)
    age = float(age)
  except ValueError:
    return None
    return sex + age

I hope everything makes sense for you and that you're able to solve the challenge! :)

Good luck!

Thank you! I didn't realize that return was not a function and that the parameters should be listed on separate lines.