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

def add(x,y): try: x = float(input("Tell me a number")) y = float(input("Tell me another number"))

What's wrong here?

trial.py
def add(x,y):
    try: 
        x = float(input("Tell me a number"))
        y = float(input("Tell me another number"))
    except ValueError
    return None
x = float(x)
y = float(y)
else:
    return (x+y)
Ben Slivinn
Ben Slivinn
10,156 Points

Im sorry, but i can't understand the logic of your code, it will be better if you could explain it, and tell me what the intention of the program. what should the program do?

but until then:

  1. Don't foger ':' in the except.

    except ValueError:
    
  2. if the 'return None' is a part of 'except' so move it one tab fwd.

    except ValueError:
      return None
    
  3. The 'else' should be part of "try/except" so you should move it one tab fwd.

  4. i can't see why you use

    x = float(x)
    y = float(y)
    

    again inside the function, if when you ask for user input you used the float function to make sure the input is float type.

1 Answer

Steven Parker
Steven Parker
231,248 Points

You have a few issues here:

  • you don't need any input statements, just work with the supplied arguments
  • check your indentation - everything inside the function must be indented
  • a try, except and else set must all be indented the same amount
  • an except statement requires a colon at the end
  • you won't need to do the float conversions twice, just once inside the try