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

need help

I need the code to finish this challenge

this is what is so far

def add(x, y): # number 1 and 2 return x + y # number 3 def add(x , y): a = float(x) b = float(y) return a + b return(float(num1) + float(num2))

trial.py
def add(x, y):    # number 1 and 2
    return x + y   # number 3
def add(x , y):
    a = float(x)
    b = float(y)
    return a + b
return(float(num1) + float(num2))

2 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

You have two add functions. There should be only one:

def add(x, y):
    return float(x) + float(y)
Tobias Helmrich
Tobias Helmrich
31,603 Points

Hey there Daniel,

you almost got it but you still have to add the try, except and else blocks. Also note that you can remove the first function and take care that you indent correctly. You have to change your arguments to floats in the try block and catch a possible ValueError.

Like so:

def add(x , y):
    try:
        a = float(x)
        b = float(y)
    except ValueError:
        return None
    else:
        return a + b

I hope that helps! :)