我只是想验证清单列表中具有其值的tic tac toe脚趾板。
board = ([1, 2, 0], [2, 1, 0], [2, 1, 2])
def print_winner(variable):
if variable == 1:
return print("Winner of this board is Player1")
else:
return print("Winner of this board is Player2")
def interpret(game_board):
if game_board[0][0] != 0:
if game_board[0][1] and game_board[0][2] == game_board[0][0]:
print(print_winner(game_board[0][0]))
elif game_board[1][0] and game_board[2][0] == game_board[0][0]:
print(print_winner(game_board[0][0]))
elif game_board[1][1] and game_board[2][2] == game_board[0][0]:
print(print_winner(game_board[0][0]))
elif game_board[0][1] != 0:
if game_board[1][1] and game_board[2][1] == game_board[0][1]:
print(print_winner(game_board[0][0]))
elif game_board[0][2] != 0:
if game_board[1][2] and game_board[2][2] == game_board[0][2]:
print(print_winner(game_board[0][0]))
elif game_board[1][2] and game_board[2][0]:
print(print_winner(game_board[0][0]))
elif game_board[1][0] != 0:
if game_board[1][1] and game_board[1][2] == game_board[1][0]:
print(print_winner(game_board[0][0]))
elif game_board[2][0] != 0:
if game_board[2][1] and game_board[2][2] == game_board[2][0]:
print(print_winner(game_board[0][0]))
else:
print("No Winner")
interpret(board)
以上代码的问题是我不知道如何在所有第一个嵌套的if语句失败后指示程序继续执行剩余的代码。因此,我的代码没有给出任何输出。
预期输出为
"No winner"
。 最佳答案
第一个问题是您错误地使用了and
。您需要在and
的任一操作数上指定条件。例如:
if game_board[0][1] == game_board[0][0] and game_board[0][2] == game_board[0][0]
要么
if game_board[0][1] == game_board[0][2] == game_board[0][0]
代替:
if game_board[0][1] and game_board[0][2] == game_board[0][0]
但是,您也可以使用
set
并检查它是否仅包含一个数字:if len({game_board[0][0], game_board[0][1], game_board[0][2]}) == 1:
关于的实际问题
一旦我的所有第一个嵌套if语句失败,如何指示我的程序继续执行其余的代码
实际上很容易。如果满足一个条件,则使用普通的
if
和return
。加上其他一些改进(您总是打印
game_board[0][0]
),它看起来像这样:def interpret(game_board):
if game_board[0][0] != 0:
if len({game_board[0][0], game_board[0][1], game_board[0][2]}) == 1:
return print(print_winner(game_board[0][0]))
elif len({game_board[0][0], game_board[1][0], game_board[2][0]}) == 1:
return print(print_winner(game_board[0][0]))
elif len({game_board[0][0], game_board[1][1], game_board[2][2]}) == 1:
return print(print_winner(game_board[0][0]))
if game_board[0][1] != 0:
if len({game_board[0][1], game_board[1][1], game_board[2][1]}) == 1:
return print(print_winner(game_board[0][1]))
if game_board[0][2] != 0:
if len({game_board[0][2], game_board[1][2], game_board[2][2]}) == 1:
return print(print_winner(game_board[0][2]))
elif len({game_board[0][2], game_board[1][1], game_board[2][0]}) == 1:
return print(print_winner(game_board[0][2]))
if game_board[1][0] != 0:
if len({game_board[1][0], game_board[1][1], game_board[1][2]}) == 1:
return print(print_winner(game_board[1][0]))
if game_board[2][0] != 0:
if len({game_board[2][0], game_board[2][1], game_board[2][2]}) == 1:
return print(print_winner(game_board[2][0]))
return print("No Winner")
之所以起作用,是因为
print
返回None
并打印到stdout
。关于python - 如果条件失败,即使在嵌套后如何继续执行剩余部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45443381/