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

https://teamtreehouse.com/library/python-basics/logic-in-python/try-and-except

Hi can anyone solve this https://teamtreehouse.com/library/python-basics/logic-in-python/try-and-except

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

2 Answers

you need to indent your code after try: and else: also, I think you need to write total = a+b after else:

Herman Brummer
Herman Brummer
6,414 Points
def add(a,b):
    try:
        float(a + b)
    except ValueError:
        return None
    else:
        return float(a + b)

Why does that not work?

because your code after try: is not what the task wants you to do. after try: you need make each interable a float using float function. not make result of (a + b) into float

Herman Brummer
Herman Brummer
6,414 Points

Thanks, strange though coz that does work and is less verbose, or more pythonic, but I guess as you pointed out it is what this marking mechanism required.