Mark
Mark

Reputation: 11

How to correct Syntax error with my elif, if statement. what am I doing wrong?

import sys

import pygame


def check_events(ship):

    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RIGHT:
            # Move the ship to the right.
            ship.moving_right = True

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_RIGHT:
            ship.moving_right = False

def update_screen(ai_settings, screen, ship):
    '''Update images on the screen and flip to the new screen.'''

Error:

File "/home/mark/python_work/game_functions.py", line 8
        elif event.type == pygame.KEYDOWN:
           ^
    SyntaxError: invalid syntax
    [Finished in 0.2s with exit code 1]

Upvotes: 0

Views: 390

Answers (3)

Python Experts
Python Experts

Reputation: 11

You cannot use elif before an if statement. Elif cannot be used without an if statement. This is the right syntax. import sys

import pygame

def check_events(ship):

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_RIGHT:
        # Move the ship to the right.
        ship.moving_right = True

elif event.type == pygame.KEYUP:
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False

def update_screen(ai_settings, screen, ship): '''Update images on the screen and flip to the new screen.'''

Upvotes: 0

Kyle Marcus Enriquez
Kyle Marcus Enriquez

Reputation: 118

You have an elif without an if preceding it. Just change the first elif to an if.

Upvotes: 0

Charles
Charles

Reputation: 3316

In your function, your first condition should start with an if.

def check_events(ship):

    if event.type == pygame.KEYDOWN:  # <--- note the change here
        if event.key == pygame.K_RIGHT:
            # Move the ship to the right.
            ship.moving_right = True

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_RIGHT:
            ship.moving_right = False

Upvotes: 2

Related Questions