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

need help

need help with this last challenge

here is my code so far

def add(x, y): # number 1 and 2 return x + y # number 3 def add(x, y): f_x, f_y = float(x), float(y) return f_x + f_y

trial.py
def add(x, y):    # number 1 and 2
    return x + y   # number 3
def add(x, y):
    f_x, f_y = float(x), float(y)
    return f_x + f_y

3 Answers

Hi there. So, the final task asks you to use a try and except block in order to catch any errors that may be brought up when attempting to convert your number to a float. In the try block, we should attempt to convert our numbers to floats, however, if a ValueError is thrown, our except block will catch it and do what we specify in the except block. In this case, the challenge is asking us to return None. Here is how it should look;

def add(num1,num2):
  try:
    return float(num1) + float(num2) #try to convert these to floats and return them
  except ValueError:                 #if there is a value error (most likely because the number is not a string)
    return None         #then just return None

If you have any further questions, please feel free to leave a comment :)

Thanks,

Haider

not working

Paste the example above into your challenge (as long as you understand it), if that still doesn't work then it is a technical error and i would advise you contact the help team by emailing them at help@teamtreehouse.com

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

That being said make sure there is nothing else in your code except the things that Haider has typed. You posted a comment on the other post that I linked to and in that post you have defined add twice.