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 trialSamantha Jordan
428 PointsHitting the wall on third challenge.
I've done a bit of googling to try and figure out where my error is. I can't seem to do it.
def add(x , y):
try:
return float(x) + float(y)
except ValueError:
return None
else:
print (x + y)
3 Answers
Jennifer Nordell
Treehouse TeacherOh you're so close. It's one of those details that are specific to challenges. They don't want you to print... they want you to return. Your code is functional, but it's one word off what they want. Take a look:
def add(x,y):
try:
return float(x) + float(y)
except ValueError:
return None
else:
return x + y
Jennifer Nordell
Treehouse TeacherHey Eugene. In reality, you don't even need the else statement there. If you'll notice at the bottom of the third step of the challenge it says something along the lines of "if you're following the structure of the video" make an else statement so I included it in the solution. But the code will pass the challenge and work outside of the challenge even without the else statement. But the reason it wouldn't say float(x) + float(y) is that we already know that the conversion to float would have failed in that case.
Look at this way. We try to add them after they're converted to float. If that fails with a value error, for example x is equal to "Hello" then we return none. But if it's an error other than value error we return x + y. But because we already know that the conversion to float failed, then converting it in the else won't work either. Hope that makes sense!
Eugene Woo
417 PointsHey Jennifer, i had the same problem as sam and your solution nailed it!
The part i don't get - why was the last line 'return x+y' instead of 'return(float(x)+float(y))' since the objective was to convert X and Y into floats before adding them. So annoying how precise the exercises can be ...