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

basil dhillo
PLUS
basil dhillo
Courses Plus Student 240 Points

how can i solve this one ???

how can i solve this problem ??

trial.py
def add(x,y):

try:

    total = float(x) + float(y)
except ValueError:
    return None
else:
    return float(total)

2 Answers

Lukas Coffey
Lukas Coffey
20,382 Points

Don't use an else: statement at the end. Your try: is turning out correct, so the function skips the return statement in the else:. Pull your return total out of the else block and you should be good!

Brandon Jaus
Brandon Jaus
13,681 Points
  1. You need to indent everything after the function definition (def) one more level.
  2. It's not necessary to have the else block. You could just write this like this:
def add(x,y):
    try:
        return float(x + y)
    except ValueError:
        return None

Of course, it is good practice to explicitly state why the function returned None instead of just implicitly letting this happen.