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

Project 1: Challenge Task 3 - Exceptions

When checked, I get the message "Bummer! 'add' did not return the correct value". When I test this code in the Workspace and assign values to num1 and num2 and then PRINT the returned value of the function, it appears to work.

Any ideas what I am doing wrong? JG

trial.py
def add(num1, num2):
    try:
        float(num1)
    except ValueError:
            return("None")
    else: 
        try: 
            float(num2)
        except ValueError:
            return("None")
        else:
            return(float(num1) + float(num2))

3 Answers

Hi Felix!

No problems with your code, I just popped it in and it passed all three tests - try refreshing the page to reload the challenge and paste it in from your example here :)

Good job! :)

Hi there!

Ah so close! It really is a small thing - it wants you to return the python type None not the string "None",

return None

Will solve it Hope it helps :)

Oh My! YES! Of course. Thank you!

Felix Ricart
PLUS
Felix Ricart
Courses Plus Student 481 Points

I'm stuck on the same problem Jim was. My code is a little different and it works in another interpreter but not on workspace?? Anyone know what is wrong with this code? The error message I'm getting is: "NameError: name 'add' is not defined"

def add(x, y):
  try:
    x = float(x)
    y = float(y)
  except ValueError:
    return None
  else:
    return x + y