3

I'm learning python. It gives syntax error in this script. I'm unable to figure out.

import exceptions
class FOUND(Exception): pass

x = [1,2,3,4,56,73,29,35,12,32,63,12,76,75,89]


while True:

    try:
        test = int(raw_input('Enter integer to be checked in list:'))
        count = -1
        for y in x:
            count += 1
            if y == test:
                raise FOUND
    except ValueError as e:
        print "Not a valid integer (%s)"%(e)
    except FOUND:
        print "Found (%d) at (%d)"%(test,count)
    else:
        print "Not found ,Appending (%d) to list at location (%d)"%(test,count+1)
        x.append(test)
    finally:
        print "The List:"
        print x
        print " "

Invalid syntax & it highlights closing double quote in this line: print "Not a valid integer (%s)"%(e)

pecker
  • 1,997
  • 3
  • 21
  • 23
  • you're using python-2.x code with a py3k interpreter. Also, changing question like this makes people who already answered looks like complete idiots. – SilentGhost Jun 14 '10 at 12:15
  • @pecker: so? if you know what interpreter you're using, why don't you fix your code accordingly? is it some sort of exercise in a basic code comprehension? – SilentGhost Jun 14 '10 at 12:17
  • @SilentGhost : Yeah, I'm doing that right now. So far I didn't know that there were changes in python3 & python2.x. I just installed python3 interpreter thinking I'm using outdated 2.x version. Python is not backward compatible. What a nightmare! – pecker Jun 14 '10 at 12:20
  • @pecker for the record, it is perfectly acceptable to stay on the 2.x series for the time being. Python 3k is a good idea for new projects that don't depend on 3rd party libs but otherwise 2.x is more likely to support the things you need and there is far more user created documentation to be found in google – Jiaaro Jun 14 '10 at 13:08
  • @pecker: "Python is not backward compatible. What a nightmare" What? How is it a nightmare. The Python web site is full of information on the lack of backward compatibility. Please read http://www.python.org/download/releases/3.1.2/ and http://www.python.org/dev/peps/pep-3000/ and http://docs.python.org/release/3.0.1/whatsnew/3.0.html. – S.Lott Jun 14 '10 at 14:41

4 Answers4

5

print without brackets is from python 2, if you are using python 3, you need to use print().

You can't format an exception as %d - %d is for integers.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
2

Try except ValueError as e:, the older syntax you use is invalid in Python 3.

Tamás
  • 47,239
  • 12
  • 105
  • 124
  • now it again throws invalid syntax but highlighting the closing quote of `print "Not a valid integer (%d)"%(e)` – pecker Jun 14 '10 at 12:10
  • @pecker: you're using python-2.x code with a py3k interpreter. I could tell which error is going to be thrown after you'll fixed this one. – SilentGhost Jun 14 '10 at 12:13
  • if you are indeed using python 3 then `print` is a function, and needs to be called like `print("Not a valid integer (%d)" % e)` or better yet `print("Not a valid integer(", int(e), ")")` (not sure if the % syntax even still works in py3k) – Carson Myers Jun 14 '10 at 12:15
1

You need an empty line between the class ... and x = ...

Wim
  • 11,091
  • 41
  • 58
  • Invalid syntax & it highlights comma in this line: 'except ValueError,e:' – pecker Jun 14 '10 at 12:06
  • 1
    The script works without a blank line there when cut-and-pasted into a Python file. If you'd type it into an interactive prompt, a blank line would be needed, however. – Pär Wieslander Jun 14 '10 at 12:07
1

Your code (cut and pasted, no alterations) works fine for me (Python 2.5).

BTW, your test = int... line should be after the try (and indented appropriately) and the %d in "Not a valid integer (%d)" should be a %s.

Syntax for exception handling has been changed for Python 3: make sure any help/tutorials you are following are for the same major version of Python you have installed. There have been signficant changes from 2.x to 3.x.

mavnn
  • 9,101
  • 4
  • 34
  • 52