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 trial

Python Python Basics (2015) Logic in Python Try and Except

Alex Durham
PLUS
Alex Durham
Courses Plus Student 456 Points

floats

can someone help me with this thanks!

trial.py
def add(num1,num2): 
    try:
        ValueError
    except:
    else:
        return a + b
    a = float (num1)
    b = float (num2)

Alex 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.

2 Answers

Steven Parker
Steven Parker
231,007 Points

As 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
Alex Durham
Courses Plus Student 456 Points

thanks 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
Steven Parker
231,007 Points

The 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
Alex Durham
Courses Plus Student 456 Points

I 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
Alex Durham
Courses Plus Student 456 Points

i just understood it thank you very much for your help!

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points
try:
  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