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

Is there a error in the code ?

def add(n,m): return n+m try: f= int(input()) g=int(input()) except ValueError: print("This is not we wanted") else: h=add(f,g)

Constantly giving Bummer! Try Again

trial.py
def add(n,m):
    return n+m
try:
    f= int(input())
    g=int(input())
except ValueError:
    print("This is not we wanted")
else:
    h=add(f,g)
    print(h)

1 Answer

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

Hi there! I suspect that you've worked on this a lot. Because your code here does not follow the requirements of the challenge. The code is going to be sent its values from another piece of code. In this case, it will be Treehouse sending in the values. The code it wants will never print out anything. It will only ever return values. And the return of the addition should happen inside the try. Take a look:

def add(x,y):
    try:
        return float(x) + float(y)
    except ValueError:
        return None

Here we define the function add. It accepts two values: x and y. We start a try block. And the first thing we're going to do is check and see if what was sent in can actually be converted to floats. If both of them can be converted to floats we return the sum of the two numbers. Otherwise, if it has a ValueError we return None per the challenge instructions.

Hope this helps! :sparkles: