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

trial.py

Can someone please assist me ? Not sure what im doing wrong.

trial.py
def add(x,y):
    try:
    return float(x) + float(y)
except ValueError:
    return None
    else:
    return float(x) + float(y)

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

indentation matters in python and inside the function it needs to look like this:

try:
    body
except:
    body
else:
    body

what makes each part called body a part of the respective block it is in is being indented. the except is different from the try part because it is at the same level of indentation. next you must understand that if what happens in the try works, the else will execute, so you don't need to do the same thing in both. if the try fails, the except executes. so in the else, you can just return the sum of the numbers. in the try, all you do is cast them to floats.