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 trialCarlos Zuluaga
690 PointsWhat's wrong in this code?
I can't figure it out what's wrong in this code.
I'd appreciate your help.
def add(num1, num2):
try:
num1 = float(num1)
num2 = float(num2)
except ValueError:
return None
else:
return num1 + num2
1 Answer
Samuel Ferree
31,722 PointsPython uses what's known as "significant whitespace." The indention isn't just there to make your code look pretty, it as a functional purpose.
You just need to indent the lines of code after your try statement, like so. (The else is also a bit redundant, so I've removed it)
def add(num1, num2):
try:
num1 = float(num1)
num2 = float(num2)
except ValueError:
return None
return num1 + num2
Carlos Zuluaga
690 PointsYes. I fixed the indention. Thanks for your help.
john larson
16,594 Pointsjohn larson
16,594 Pointsthe else might be redundant, but the challenge asks for it. Although it's interesting how Samuel made it work without one. Bringing the final return out to the same indent as try and except just makes it the next thing to happen. Fun stuff. :D