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

Yann Husseini
Yann Husseini
679 Points

I am stuck in this challenge.

Can someone tell me where is my error?

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

add(9, 8)

3 Answers

Hi there!

try is outside the function definition. Remember that functions don't do anything until you call them by putting () on the end. So this says to python try defining a function... which doesn't make sense to python. Moreover you get a conflict - the block that follows try needs to be indented, and the code inside the function needs to be indented, but the except needs to line up with the try and be inside the function... so it's all too much for python and you get an error.

The fix: just move the try inside the function.

Hope it helps :)

simhub
simhub
26,544 Points

hi Yann
this one worked for me:

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

just but the 'try' statement inside your function - like Jon said ;)

Yann Husseini
Yann Husseini
679 Points

Hello everyone,

Thanks for your help!