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 trialSarah Cordier
1,399 PointsTRY AND EXCEPT
I have been stuck on this for days and I can't seem to get help from my classmates. I am very confused about how to use try and except blocks. If someone could help explain, that would be great. Thanks!
def add (num1, num2):
try:
return float(num1) + float(num2)
except ValueError:
return none
2 Answers
Anish Walawalkar
8,534 PointsHey Sarah, you made 2 very small mistakes:
- You forgot to indent the block of code inside the add function
- It should be None not none
in code it looks like this:
def add(num1, num2):
try:
return float(num1) + float(num2)
except:
return None
And you used the try and perfect blocks perfectly. As the names suggest you run some piece of code in the try block which may or may not raise an exception. when it does raise an exception it enters the except block to perform the exception handing code. :)
tomasvukasovic
24,022 PointsFirst of all they should be indented, and Try and except is used to run a piece of code that you know can through an error and if that error happens then you can run another piece of code regarding that error, like printing a message that says " you can type letters only numbers "