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

Katharina Rolfs
Katharina Rolfs
1,245 Points

How to solve the challenge task correctly

My piece of code is working correctly in the python shell, but is not accepted in the challenge task. It tells me that I have a name error and 'add' is not defined, which is the function I had to create in the first part of the task, where it works nicely.

Thanks for your help, Katharina

trial.py
def add(a1, a2):
    try:
        c = float(a1)+float(a2)
    except ValueError:
        return none
    else:
        return c

2 Answers

andren
andren
28,558 Points

The problem stems from this line:

return none

The value you are meant to return in None, not none, the capitalization matters. Since none is not a valid Python value your code results in the code checker crashing as it tries to verify your code, that's why it gives you the misleading error message about the function not being defined.

Since that bit of code only executes if you pass in a value that results in a ValueError being thrown the code will appear to work just fine if you pass it two valid numbers. Which I'm guessing is what you did while testing the code in the Python shell.