Python 意外字符

Python Unexpected character

嘿,我在这个 if 语句的行错误后得到了这个意外的字符

if (StartSquare % 10 != 0 and StartSquare % 10 != 9 and
      StartSquare \ 10 != 0 and StartSquare \ 10 != 9 and
      FinishSquare % 10 != 0 and FinishSquare % 10 != 9 and
      FinishSquare \ 10 != 0 and FinishSquare \ 10 != 9):
    ValidMove = True

\ 是一个转义字符,它告诉 Python 可能需要忽略 下一个 字符。在 Python 代码中,在字符串文字之外,它用于创建逻辑行延续:

foo = 'This is a longer string ' \
      'wrapped across more than one line'

在行中间使用没有实际意义;然后它被忽略。对于 Python,您的代码如下所示:

if (StartSquare % 10 != 0 and StartSquare % 10 != 9 and
      StartSquare 10 != 0 and StartSquare 10 != 9 and
      FinishSquare % 10 != 0 and FinishSquare % 10 != 9 and
      FinishSquare 10 != 0 and FinishSquare 10 != 9):

这是无效的语法; StartSquare 之后的 10 是意外的。

如果你想使用 除法 则使用正斜杠; /.

由于您可能在此处处理整数,因此您确实会使用 floor 除法//。在 Python 2 / 中,当两个操作数都是整数时进行底除法,但是如果 StartSquareFinishSquare 是一个浮点数,你会得到常规除法,而 // 即使在那种情况下也明确坚持楼层划分。

你的表情比较难'grok';也许您可以测试特定数字:

if StartSquare not in (0, 9, 90, 99) and FinishSquare not in (0, 9, 90, 99):