【发布时间】:2022-01-20 19:21:13
【问题描述】:
在 python 中,我当前的代码可以工作到某个点。我有另一个名为check_X_win_status() 的函数,它的作用与下面的函数相同,只是它检查的是1,而不是-1。有人对如何使它更紧凑有任何想法吗?此外,即使 game_status = -1, 1,-1, 0, 0, 0, 0, 0, 0,我有时也会收到代码打印“win”的错误
game_status = [-1,-1,-1,0,0,0,0,0,0]
def check_O_win_status():
if game_status[0] and game_status[1] and game_status[2] == -1:
print("O wins!")
if game_status[3] and game_status[4] and game_status[5] == -1:
print("O wins!")
if game_status[6] and game_status[7] and game_status[8] == -1:
print("O wins!")
if game_status[0] and game_status[3] and game_status[6] == -1:
print("O wins!")
if game_status[1] and game_status[4] and game_status[7] == -1:
print("O wins!")
if game_status[2] and game_status[5] and game_status[8] == -1:
print("O wins!")
if game_status[0] and game_status[4] and game_status[8] == -1:
print("O wins!")
if game_status[2] and game_status[4] and game_status[6] == -1:
print("O wins!")
【问题讨论】:
-
if game_status[0] and game_status[1] and game_status[2] == -1这不是你想的那样。见this question