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

What am I doing wrong?

I have tried everything...

trial.py
def add (scooby, doo):

    try:    
    return float(scooby) + float(doo)
except ValueError:
    return(None)
else:
    return(float(scooby) + float(doo))

3 Answers

Jonathan Corona
Jonathan Corona
716 Points

I spent the last hour working on the same exact problem, hopefully this will help you with syntax:

def add(scooby, doo):
    try:    
        return(float(scooby) + float(doo))
    except ValueError:
        return None

You do not need an else statement if you have two returns. You're main issue was that you did not have the proper indents and you you needed more parenthesis a la: return(float(scooby) + float(doo)).

Steven Parker
Steven Parker
231,269 Points

The parentheses around the entire return expression are actually not necessary (but they don't hurt).

Steven Parker
Steven Parker
231,269 Points

It looks like you need to adjust your indentation a bit:

  • everything that is part of a function must be indented more than the "def" line
  • a "try", "except" and "else" must all be indented the same amount (they must line up)
  • anything that is part of those must be indented more

Also, you won't need an "else" if both the "try" and "except" perform a "return".

I'l bet you can get it now without an explicit spoiler.

Jonathan Corona
Jonathan Corona
716 Points

Thank you, it didn't fail me on my answer so I would not have none that about the parenthesis unless you had mentioned it.