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

Solved it yesterday, I am trying to solve it today as well but I keep doing something wrong for some reason.

def add(a, b,):

try:
    return(float(a) + float(b))
except ValueError:
    return None
else:
    return(float(a) + float(b))

? thnx :)

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your code and logic are fine. The syntax is spot on. However, the indentation is incorrect. The declaration of the of the method begins a block. Everything after must be indented to be a part of this block. So, if I simply rework your indentation, it passes the last step.

def add(a, b,):

    try:
        return(float(a) + float(b))
    except ValueError:
        return None
    else:
        return(float(a) + float(b))

Keep in mind, this is your code, only properly indented! Hope this helps! :sparkles:

Sneha Nagpaul
Sneha Nagpaul
10,124 Points

I don't understand the else statement in this code. Isn't it redundant?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Yes, Sneha Nagpaul. It is redundant and not needed at all. In fact, the instructions on step 3 hint at this. There is some part in the video where they use the else statement so in step 3 you'll see this:

If you're following the structure from the videos, add an else: for your final return of the added floats.

But, in reality, the else statement can be removed entirely :thumbsup:

Diar Selimi
Diar Selimi
1,341 Points

maybe you wan't to call your defined function in the first return like return add(param1, param2)

def add(a, b,):

try:
    return add(float(1), float(2))
except SomeException as exception:
    return null
finally:
    return something