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

标签 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]

最佳答案

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

关于python - 如何用我的elif if语句纠正语法错误。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54565611/

相关文章:

python - 编写此表达式的更 pythonic 方式?

python - 更改 pandas 中数据框的堆叠

python - if else 在 for 循环下工作 != but ==

javascript - 如何根据 JavaScript/jQuery 中查询字符串的值执行 if 条件?

c# - 在输出中出现错误消息后重新启动 while 循环

sql - SQL Server中的更新语句中的错误

python - 需要此 'Invalid Syntax error'的帮助

Python 评估 : What's wrong with this code?

python - 输入与 raw_input : Python Interactive Shell Application?

python - Scipy 优化算法? (用于最小化神经网络成本函数)- python