1

以下代码给了我错误:

    if self.from_position in self.supporters or 
                                               ^
SyntaxError: invalid syntax

代码:

def validate_move_piece(self):
    if self.from_position in self.supporters or 
    (self.turn == 'X' and self.from_position == self.player_x) or 
    (self.turn == 'Y' and self.from_position == self.player_y) or 
    (self.last_position == self.from_position):
        return True
    else:
        self.error = ERROR_MOVE_FORMAT 
        return False
4

3 回答 3

4

在整个 if 语句条件周围加上括号,如下所示:

>>> if (True or
... False or
... True):
...     print 'woo'
... 
woo

(这适用于 python 文件以及解释器。)

于 2012-10-15T04:30:51.673 回答
4

在行尾添加反斜杠 \。

if 1==1 and\
2==2:
  print 'hi'
于 2012-10-15T04:31:33.567 回答
2

您可以使用()分隔多行。

例如,您的代码将是:

def validate_move_piece(self):
    if (self.from_position in self.supporters or 
    (self.turn == 'X' and self.from_position == self.player_x) or 
    (self.turn == 'Y' and self.from_position == self.player_y) or 
    (self.last_position == self.from_position)):
        return True
    else:
        self.error = ERROR_MOVE_FORMAT 
        return False

看,我已经if用一个吞没了这个声明()

于 2012-10-15T04:32:20.827 回答