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 trialAnne Manning
5,415 PointsI don't know how to solve this assignment.
I am having trouble with the try and except blocks of code in challenge 3 of 3. any direction would be great.
def add(a, b):
return float(a) + float(b)
try:
fl_a = float(a)
fl_b = float(b)
except ValueError:
return None
else:
return fl_a + fl_b
3 Answers
Attila Farkas
9,323 PointsOne way to make your code work would be to move the try-except block inside the add function (you also don't need the else clause):
def add(a, b):
try:
fl_a = float(a)
fl_b = float(b)
except ValueError:
return None
return fl_a + fl_b
Gianmarco Mazzoran
22,076 PointsHi,
i think you're not passing the last task because it asking you to add the try:
method before you convert your arguments into floats.
In your code snippet you convert a
and b
and return
them two times.
Instead you could do this to keep the things clean:
def add(arg1, arg2):
try:
return float(arg1) + float(arg2)
except ValueError:
return None
Attila Farkas
9,323 PointsAnne,
I just tried the code I suggested, and it passed the test. Make sure the indentation levels are consistent with the code above (try, except, last return lines must have same indentation).
Anne Manning
5,415 PointsAnne Manning
5,415 Pointsunfortunately, I am still getting the error message: "Bummer, try again"