如何用我的elif if语句纠正语法错误。我究竟做错了什么?

时间:2019-02-07 02:51:34

标签: python if-statement syntax-error

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.'''

错误:

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]

3 个答案:

答案 0 :(得分:2)

在您的职能中,您的第一个条件应以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

答案 1 :(得分:0)

您有一个elif,前面没有if。只需将第一个elif更改为if

答案 2 :(得分:0)

不能在if语句之前使用elif。如果没有if语句,则不能使用Elif。 这是正确的语法。 导入系统

导入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设置,屏幕,出厂):     '''更新屏幕上的图像,然后切换到新屏幕。'