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 trialAlex Durham
Courses Plus Student 456 Pointsfloats
can someone help me with this thanks!
def add(num1,num2):
try:
ValueError
except:
else:
return a + b
a = float (num1)
b = float (num2)
2 Answers
Steven Parker
231,248 PointsAs I said before, here's a few hints:
- everything inside a function must be indented
- a try and the corresponding except and optional else must all be indented to the same level
- the code that might cause an exception must be between the try and the except
- the code inside the try section must be indented further than the try itself
Alex Durham
Courses Plus Student 456 Pointsthanks for your help but i understand the first 2 but i am struggling to understand your 3rd bullet point what is the code that causes the exception Thanks
Steven Parker
231,248 PointsThe lines where you convert to float might cause an exception if the input is bad.
And ValueError belongs with the except statement.
And don't forget to do what the challenge said when you have an exception.
Alex Durham
Courses Plus Student 456 PointsI really appereciate you helping me this what i currently have i am just struggling to understand what to put where def add(num1,num2): try: count = int except ValueError: return None else: a = float (num1) b = float (num2) return a + b
Alex Durham
Courses Plus Student 456 Pointsi just understood it thank you very much for your help!
james south
Front End Web Development Techdegree Graduate 33,271 Pointstry:
do something that might cause an error
except Error(s)ThatMightHappen:
do something to deal with error(s)
else:
do something if what was tried was successful and the error(s) weren't thrown
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsAlex Durham
If an answer isn't helping you then you should go back and leave a comment to your original question rather than posting your question again. You've posted several duplicates to multiple code challenges.