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

Try Block?

I'm not quite sure what the try block is exactly. also can someone give me a clear example how to solve this problem?

I've seen several examples on a page i stumbled on, and all of them make sense, but the syntax doesn't work.

please and thank you

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

2 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya Richard! Your code is almost there but just make couple of following changes. First off, "total" is not defined. Secondly, remove "=" from both "return" statements. Third, remove the last statement in your code and move the "return(a + b)" expression in else block. Do these chages in your code and you're good to go. (:

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

i don't think i followed your instructions correctly haha

Ari Misha
Ari Misha
19,323 Points

Alright! Try this:

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

thankyou it was that easy haha thankyouuu