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 trialchris Roach
3,475 Pointsi think im writing this code right but its not giving me anything
i feel like ive got this code all set up correctly to finish the problem but its just not working
def add(num1, num2):
try:
num1 = float(num1)
num2 = float(num2)
except ValueError:
return None
else:
return(num1 + num2)
2 Answers
Zach Swift
17,984 PointsI think he was looking for just a try except. You could just try returning the added floats at first and if there is an exception, then return None.
def add(num1, num2):
try:
return float(num1) + float(num2)
except ValueError:
return None
Tobias Helmrich
31,603 PointsHey Chris,
you almost got it right! The only problem in your code is your indentation. In particular the indentation in your try and else blocks. If you indent everything correctly there it should work just fine! :)
Like so:
def add(num1, num2):
try:
num1 = float(num1)
num2 = float(num2)
except ValueError:
return None
else:
return(num1 + num2)
I hope that helps, good luck! :)