Syntax error in python:

I am learning python. This gives a syntax error in this script. I can not understand.

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 " "

The syntax is not valid and it highlights the double quote closing on this line: print "Not a valid integer (%s)"%(e)

+3
source share
4 answers

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

You cannot format an exception as %d- %dfor integers.

+5
source

Try it except ValueError as e:, the old syntax you are using is not valid in Python 3.

+2
source

class ... x = ...

+1

( , ) (Python 2.5).

By the way, your line test = int ... should be after the attempt (and, respectively, indented), and% d in "Invalid integer (% d)" should be% s.

The syntax for handling exceptions has been changed for Python 3: make sure that you are using any help / manuals for the same major version of Python that you installed. Significant changes have been made from 2.x to 3.x.

+1
source

Source: https://habr.com/ru/post/1749922/


All Articles