Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.