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

ujjwal shankar
ujjwal shankar
428 Points

The error message just says "Bummer ! Try again!

def add(arg1,arg2): try: float(arg1) float(arg2) except ValueError: return none else: return float(arg1) + float(arg2)

not sure why this code is wrong

trial.py
def add(arg1,arg2):
    try:
        float(arg1)
        float(arg2)
        except ValueError:
            return none
        else:
             return float(arg1) + float(arg2)

3 Answers

Manish Giri
Manish Giri
16,266 Points

Your indentation is wrong, except and else seem to be inside the try block, whereas they should be on the same level -

try:
    # code
except:
    # code
else:
    # code
ujjwal shankar
ujjwal shankar
428 Points

hi Manish ,

Thanks for response , yes i figured that..

Cheers, Ujjwal

carlos florez
carlos florez
9,664 Points

Your try block should suffice without the need for an else statement. Try returns the addition between both arguments, shall we pass something other than an "integer" or number as arguments a ValueError is "raised", in this case we are already using the exception as an else statement, "if ValueError is presented do this instead", which is to return None

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