首页 > 解决方案 > Odd sytax error in python: Invalid Syntax

问题描述

So for whatever reason I keep getting a syntax error and I can't seem to figure out why. When I comment out the line that is giving me the error it propagates through, but when I put the troublesome bit in a new file it carries through and I'm not seeing anything wrong above or below in all of the code syntactically.

def removeBlock(pos, blocks):
    return blocks[pos] -= 1
def addBlock(pos, blocks):
    return blocks[pos] += 1
def left(pos):
    return pos -= 1
def right(pos):
    return pos += 1
def done():
    return "quit"

actions = {
    "P": removeBlock,
    "D": addBlock,
    "L": left,
    "R": right,
    "X": done
}

The error is this:

SyntaxError: invalid syntax

it appears on the second line of this code.

标签: pythonsyntax

解决方案


Python-=运算符不提供返回值。也用于+=等。

尝试:

def removeBlock(pos, blocks):
    blocks[pos] -= 1
    return blocks[pos]

或者你的意思是不改变块?

def removeBlock(pos, blocks):
    return blocks[pos] - 1

推荐阅读