Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
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):
return(float(sex)+float(age)) except ValueError: return(None)
def add(sex, age): try: return(float(sex)+float(age)) except ValueError: return(None)
Tobias Helmrich31,602 Points
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): try: sex = float(sex) age = float(age) except ValueError: return None else: return sex + age
I hope everything makes sense for you and that you're able to solve the challenge! :)
Thank you! I didn't realize that return was not a function and that the parameters should be listed on separate lines.