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 trialKong Cheong
633 PointsPython: Try and Except Challenge Task 3/3
Hi, I have tried dozens of times. I went and looked at the discussion and found an answer but it is still returning an error for me. Here is my answer:
def add(x+y):
try:
a = float(x)
b = float(y)
except ValueError:
return None
else:
return True
If anyone knows what is wrong, feel free to let me know.
Thanks.
[MOD: added ```python markdown formatting -cf]
6 Answers
Daniel Santos
34,969 PointsThis is a tricky one.
Here is the code, please analyze it, if you still don't understand comment me.
def add(a, b):
try:
return float(a) + float(b)
except ValueError:
return None
else:
return float(a) + float(b)
-Dan
Chris Freeman
Treehouse Moderator 68,441 PointsAfter reformatting your code, there are three errors: argument list, indentation of except
and else
statements, and the return value:
def add(x, y): # <-- changed from x+y
try:
a = float(x)
b = float(y)
except ValueError:
return None
else:
return a + b # <-- fix return value
Kong Cheong
633 PointsHi Dan,
You are awesome, it works. I was very close on the else portion but didn't know about the part under try: return float(a) + float(b). One question tho, what is the "as e" mean?
Daniel Santos
34,969 PointsThank you. e in this case is an instance of the ValueError object created by the exception. I will be honest, I might went to far writing that. I believe it was not required in the challenge. But answer, with e now you can access information about with this exception happened. Did get me?
I have a question for you, did Kenneth Love mentioned something like that in the lessons? I just haven't watch the new videos.
Chris Freeman
Treehouse Moderator 68,441 PointsI believe the first use of "as e" shows up in the Python Testing course.
Kong Cheong
633 PointsKenneth actually didnt mentioned it in this video. the only e i saw was the "3.54334343e17". The video is great and i just find the practice work a little too advance for the video's content. Some were relevant and others are a little out there. I really appreciate you helping me, i was stuck and there is no way to get a hint or a sample in the practice space.
Daniel Santos
34,969 PointsOk, and remember that learning is a a repeat-and-fail process. When you struggle is when you are actually learning. And I want you to ignore the "as e" (for the moment). I am pretty sure you will get a better introduction to that concept in the videos. Good luck!
Kong Cheong
633 PointsHi Chris,
I actually did this format you shown here and it didnt work which was frustrating. The first mistake was accidental (x,y), I typed it incorrectly. strange but Dan's answer worked instantly which is crazy to me.
Chris Freeman
Treehouse Moderator 68,441 PointsHi Kong,
I have tested my corrected version of your code and it passes Task 3. Perhaps you have indentation errors lingering in your code. You seem to be using 5-space indentation instead of the typical 4-space indentation. Sometimes the grader is picky on the indentation.
Kong Cheong
633 PointsHi Chris,
Gotcha, it was very confusing on why it keep telling me "bummer" but not quite telling me where it went wrong. I appreciate you testing it. At least I know I am not crazy. Thanks Chris.