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 trialDaniel Holmes
2,765 Pointsneed 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
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
Haider Ali
Python Development Techdegree Graduate 24,728 PointsHi 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
Jennifer Nordell
Treehouse TeacherI've posted what I feel is a decent explanation of this problem here https://teamtreehouse.com/community/hi-learning-about-python-in-python-basics-stuck-on-this-lesson. Take a look. Hope it helps!
Daniel Holmes
2,765 Pointsnot working
Haider Ali
Python Development Techdegree Graduate 24,728 PointsPaste 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
Treehouse TeacherI agree with Haider Ali. Both of us have given code that should pass with a copy/paste.
Jennifer Nordell
Treehouse TeacherThat 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.