【问题标题】:Can't use equal operator不能使用相等运算符
【发布时间】:2019-11-06 00:29:45
【问题描述】:

当我尝试使用 '=' 运算符让 python 将参数的值与用户输入进行比较时,它告诉我它无法处理语法错误。

无论出于何种原因,将所有等号更改为“is”运算符可修复此错误。虽然我觉得这是一种让我的程序出现难以置信的错误而不是正确修复的好方法。

def movePieces1(initialSpot,finalSpot):
    initialSpot = input('Move which piece?: ').upper()
    finalSpot = input('To which place?: ').upper()
    if initialSpot = 'A1' and finalSpot = row1[0] or row1[2,5]:
        print('Invalid Move')

    elif initialSpot = 'A2' and finalSpot = row1[1] or row1[3,5]:
            print('Invalid Move')

错误:

文件“pown-chess.py”,第 81 行
如果 initialSpot = 'A1' 和 finalSpot = row1[0] 或 row1[2,5]:
^ SyntaxError:语法无效

【问题讨论】:

  • 使用== 比较值。 = 是赋值。

标签: python-3.6


【解决方案1】:

要检查是否相等,您需要使用==

= 将名称分配给值。

把它改成这样:

def movePieces1(initialSpot,finalSpot):
    initialSpot = input('Move which piece?: ').upper()
    finalSpot = input('To which place?: ').upper()
    if initialSpot == 'A1' and finalSpot == row1[0] or row1[2,5]:
        print('Invalid Move')

    elif initialSpot == 'A2' and finalSpot == row1[1] or row1[3,5]:
            print('Invalid Move')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    相关资源
    最近更新 更多