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 trialDaron George
700 PointsPython Question
I need help figuring out what I am doing wrong.
Add a try block before where you turn your arguments into floats.
Then add an except to catch the possible ValueError. Inside the except block, return None.
def add(ab1,ab2):
try:
return float(ab1) + float(ab2)
except ValueError:
return ('none')
else:
return float(ab1) + float(ab2)
1 Answer
Jeremiah Bushau
24,061 PointsHey there, looks like you need to fix the indentation of the except and else blocks. Also instead of returning 'none' in your except block return None. Here is a helpful stackoverflow question about the None value http://stackoverflow.com/questions/19473185/what-is-a-none-value.
def add(ab1,ab2):
try:
return float(ab1) + float(ab2)
except ValueError:
return None
else:
return float(ab1) + float(ab2)
Daron George
700 PointsDaron George
700 PointsThanks for your help!