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 trialMonica Dhulipudi
4,075 Pointshttps://teamtreehouse.com/library/python-basics/logic-in-python/try-and-except
Hi can anyone solve this https://teamtreehouse.com/library/python-basics/logic-in-python/try-and-except
def add(new,old):
try:
a = float(new)
b = float(old)
total = a+b
except ValueError:
return None
else:
return total
add(1,1)
2 Answers
taejooncho
Courses Plus Student 3,923 Pointsyou need to indent your code after try: and else: also, I think you need to write total = a+b after else:
Herman Brummer
6,414 Pointsdef add(a,b):
try:
float(a + b)
except ValueError:
return None
else:
return float(a + b)
Why does that not work?
taejooncho
Courses Plus Student 3,923 Pointsbecause your code after try: is not what the task wants you to do. after try: you need make each interable a float using float function. not make result of (a + b) into float
Herman Brummer
6,414 PointsThanks, strange though coz that does work and is less verbose, or more pythonic, but I guess as you pointed out it is what this marking mechanism required.