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

vvsr sashank
vvsr sashank
1,393 Points

what do I have to do in task 3?

def add(num1,num2): return float(num1)+float(num2)

trial.py
def add(num1,num2):
try:
    return float(num1)+float(num2)
except ValueError:
    return None
else:
    return None
Nicolas Bassano
Nicolas Bassano
2,125 Points

Hi!

mmmm I wonder how did you get to task 3??

Can you share the code for task 1 and 2?

vvsr sashank
vvsr sashank
1,393 Points

def add(num1,num2):

try: return float(num1)+float(num2)

except ValueError: return None

else: return None

2 Answers

Steven Parker
Steven Parker
231,269 Points

Now you need a try block to allow the function to handle errors gracefully.

There's a good example of putting a try sequence together about 1 minute into the video The Exception to the Rule. The main difference in the challenge is that you won't be using print, just another return with a *None value.

You can leave your current return as-is, and you won't need the else.

Nicolas Bassano
Nicolas Bassano
2,125 Points

As Steven said: the last else is not nedded

Plus, notice that you have an indentation problem on line 2 hence indentation problems for the rest of the code.

You were nearly there, keep at it.

BTW, the code should look like this.......

def add(num1,num2):
    try:
        return float(num1) + float(num2)
    except ValueError:
        return(None)