1

I'm getting a SyntaxError: invalid syntax in response to the following code when I run in it IDLE, but not in PythonTutor. Can anyone help with why?

def apples(x, y):
    try:
        result = x/y
    except ZeroDivisionError, e:
        print "division by zero!" + str(e)
    else:
        print "result is", result
    finally:
        print "executing finally clause"

apples(3, 4)

The SyntaxError is associated with the 'apples' in apples(3,4)

5
  • Fix your indention. It's hard to tell what the problem is here. Commented Jul 29, 2014 at 17:53
  • 2
    What exact error do you get? Commented Jul 29, 2014 at 17:55
  • Just did that - I'm new to Stack Overflow's formatting. Commented Jul 29, 2014 at 17:55
  • The exact error is: SyntaxError: invalid syntax Commented Jul 29, 2014 at 17:56
  • funny, it is happening for me too, are you pasting the code in? Commented Jul 29, 2014 at 18:07

2 Answers 2

1

The code that you are showing should run correctly in Python 2, but will fail in Python 3 due to the print statements.

Here is how you might make it work in Python 3, assuming that is your problem.

def apples(x, y):
    try:
        result = x/y
    except ZeroDivisionError, e:
        print("division by zero!" + str(e))
    else:
        print("result is", result)
    finally:
        print("executing finally clause")

apples(3, 4)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your input - I see the difference between the two. However, I'm in Python 2.7.8; is your correction still applicable?
@user3882253, Are you absolutely sure that that is the version your IDLE is running?
Absolutely: Python 2.7.8, IDLE 2.7.8, & Tk 8.5.15. I'm on a MacBook Pro w/ 10.9.4
Does my answer fix the issue?
0

The only way I could replicate you error was pasting the code into idle including apples(3, 4).

If I pasted the function first then ran the function with apples(3, 4), it works fine.

>>> def apples(x, y):
        try:
            result = x/y
        except ZeroDivisionError, e:
            print "division by zero!" + str(e)
        else:
            print "result is", result
        finally:
            print "executing finally clause"
apples(1,3)
SyntaxError: invalid syntax
>>> 

>>> def apples(x, y):
        try:
            result = x/y
        except ZeroDivisionError, e:
            print "division by zero!" + str(e)
        else:
            print "result is", result
        finally:
            print "executing finally clause"


>>> apples(2,3)
result is 0
executing finally clause

2 Comments

Ahhhhh Thanks so much! This totally worked. I will remember not to paste into the shell both the code and the inputs at the same time.
No worries, you're welcome. I would highly recommend using Ipython, there are numerous features that make working with python even more fun. You can just copy code and type paste in the shell and it will run you code, that is just one of many cool features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.