Syntax error "except pygame.error"

I am python and pygame noob, looked at the tutorial on loading sprites into my game, and I get a syntax error for this line of code

    except pygame.error, message:
                   ^
    SyntaxError: invalid syntax

This is the whole block of code:

def load_image(self, image_name):

    try:
        image = pygame.image.load(image_name)

    except pygame.error, message:

        print "Cannot load image: " + image_name
        raise SystemExit, message

    return image.convert_alpha()

I did not check if the tutorial was for python 3.4.2 or not, did the syntax change?

Or is there something wrong with my code?

+4
source share
2 answers

You should use as messagein python3 and raise SystemExit(message):

def load_image(self, image_name):  
    try:
        image = pygame.image.load(image_name)    
    except pygame.error as message:   
        print("Cannot load image: " + image_name)
        raise SystemExit(message)    
    return image.convert_alpha()

Also printis functionin python3, so you need parsers.

+5
source

The entry is except pygame.error, message:valid only in Python 2. In Python 3, you should write:

except pygame.error as message:
+2
source

All Articles