井字游戏 - 无法分配给函数调用
Tic Tac Toe Game- Can Not Assign to Function Call
到目前为止,我正在 python 上制作井字游戏,我已经想出了一个非常可靠的代码,但我在某一点上遇到了一个我无法修复的错误。
代码如下:
from random import randint
List=[[0,0,0],[0,0,0],[0,0,0]]
def board(List):
print('=== Tic Tac Toe ===')
for i in range(3):
print(' ',List[i][0],' ',List[i][1],' ',List[i][2])
def usr_choice(i,f,List):
while List[i][f]!=0:
i,f =eval(input('Please enter correct choice i or f: '))
List[i][f]=2
def comp_choice(List):
i=randint(0,2),f=randint(0,2)
while List[i][f]!=0:
i=randint(0,2),f=randint(0,2)
List[i][f]=1
def decision(List):
if [List[i][i] for i in range(3)]==[2,2,2] or \
[List[2-i][i] for i in range(3)]==[2,2,2]:
print('You win!')
return True
elif[List[i][i] for i in range(3)]==[1,1,1] or \
[List[2-i][i] for i in range(3)]==[1,1,1]:
print('You lose!')
return True
else:
for i in range(3):
if [List [i][:]for i in range(3)]==[2,2,2] or \
[List[f][i] for j in range(3)]==[2,2,2]:
print('You win!')
return True
elif [List [i][:]for i in range(3)]==[1,1,1] or \
[List[f][i] for j in range(3)]==[1,1,1]:
print('You lose!')
return True
这是我在 def comp_choice(List):
下收到的错误
i=randint(0,2),j=randint(0,2)
^
SyntaxError: cannot assign to function call
要在同一赋值中声明多个变量,请执行:
i, j = randint(0, 2), randint(0, 2)
要在不同的赋值中但在同一行中声明多个变量,请执行:
i = randint(0, 2); j = randint(0, 2)
到目前为止,我正在 python 上制作井字游戏,我已经想出了一个非常可靠的代码,但我在某一点上遇到了一个我无法修复的错误。
代码如下:
from random import randint
List=[[0,0,0],[0,0,0],[0,0,0]]
def board(List):
print('=== Tic Tac Toe ===')
for i in range(3):
print(' ',List[i][0],' ',List[i][1],' ',List[i][2])
def usr_choice(i,f,List):
while List[i][f]!=0:
i,f =eval(input('Please enter correct choice i or f: '))
List[i][f]=2
def comp_choice(List):
i=randint(0,2),f=randint(0,2)
while List[i][f]!=0:
i=randint(0,2),f=randint(0,2)
List[i][f]=1
def decision(List):
if [List[i][i] for i in range(3)]==[2,2,2] or \
[List[2-i][i] for i in range(3)]==[2,2,2]:
print('You win!')
return True
elif[List[i][i] for i in range(3)]==[1,1,1] or \
[List[2-i][i] for i in range(3)]==[1,1,1]:
print('You lose!')
return True
else:
for i in range(3):
if [List [i][:]for i in range(3)]==[2,2,2] or \
[List[f][i] for j in range(3)]==[2,2,2]:
print('You win!')
return True
elif [List [i][:]for i in range(3)]==[1,1,1] or \
[List[f][i] for j in range(3)]==[1,1,1]:
print('You lose!')
return True
这是我在 def comp_choice(List):
下收到的错误 i=randint(0,2),j=randint(0,2)
^
SyntaxError: cannot assign to function call
要在同一赋值中声明多个变量,请执行:
i, j = randint(0, 2), randint(0, 2)
要在不同的赋值中但在同一行中声明多个变量,请执行:
i = randint(0, 2); j = randint(0, 2)