4

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?

Fatnomen
  • 41
  • 1
  • 2

2 Answers2

5

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.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
2

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

except pygame.error as message:
jwodder
  • 54,758
  • 12
  • 108
  • 124