Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error, "except pygame.error"

I am a python and pygame noob, looked up a tutorial for loading sprites in to my game, and I'm getting syntax error for this this line of code

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

This is the entire 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 didn't check if the tutorial was for python 3.4.2 or not, has the syntax changed?

Or is there something else wrong with my code?

like image 355
Fatnomen Avatar asked Mar 16 '23 21:03

Fatnomen


2 Answers

You have to use as message in 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 print is a function in python3 so you need parens.

like image 184
Padraic Cunningham Avatar answered Mar 19 '23 09:03

Padraic Cunningham


Writing except pygame.error, message: is only valid in Python 2. In Python 3, you must instead write:

except pygame.error as message:
like image 31
jwodder Avatar answered Mar 19 '23 10:03

jwodder