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

value error not changing color and is my solution wrong ?

my valueerror is not changing color.. is there something im doing wrong coz when i ask it to check my work it just says bummer

trial.py
def add (a, b):
    try:
        a = float(a)
        b = float(b)
        except ValueError:
            return None
        else:
            return a+b

1 Answer

Rich Zimmerman
Rich Zimmerman
24,063 Points

Try/except/else blocks have to have the same indentation. Basically what python thing its happening is you're putting your except block INSIDE of your try block.

You're also not returning a + b in your try block, instead you have it in your "else" block. So when your code runs, you won't return a + b unless your try block fails, and a ValueError is not being thrown. Check out what i have and see the difference in indentation from your code.

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