为什么代码只有在不在 Python 中的函数中时才有效?
Why does the code only works when it's not in a function in Python?
我认为这是一个全局或局部错误,但我不明白。
def who_wins_when_player_3(player):
if player == 3:
amount_triangles = np.count_nonzero(board == 3)
if amount_triangles == 3 or 5 or 7:
player = 2
else:
player = 1
这里不行:
# vertical win check
for col in range(BOARD_COLS):
if board[0][col] == player and board[1][col] == player and board[2][col] == player or board[3][col] == player and board[1][col] == player and board[2][col] == player:
who_wins_when_player_3()
print(f"Player {player} wins")
return True
这里有效:
# vertical win check
for col in range(BOARD_COLS):
if board[0][col] == player and board[1][col] == player and board[2][col] == player or board[3][col] == player and board[1][col] == player and board[2][col] == player:
if player == 3:
amount_triangles = np.count_nonzero(board == 3)
if amount_triangles == 3 or 5 or 7:
player = 2
else:
player = 1
print(f"Player {player} wins")
return True
哪里出错了?
此致
在函数内部赋值不会做任何事情,除非您 return
该值。尝试此版本的函数,其中返回 player
的获胜值:
def who_wins_when_player_3(player):
if player == 3:
amount_triangles = np.count_nonzero(board == 3)
if amount_triangles == 3 or 5 or 7:
return 2
else:
return 1
return player # if we don't do this, the function will return None if player != 3
然后让调用者将其分配给自己范围内的player
:
# vertical win check
for col in range(BOARD_COLS):
if board[0][col] == player and board[1][col] == player and board[2][col] == player or board[3][col] == player and board[1][col] == player and board[2][col] == player:
player = who_wins_when_player_3(player) # pass player in and reassign it
print(f"Player {player} wins")
return True
此代码可能还有其他问题,但希望这至少可以阐明从函数返回值的工作原理。
我认为这是一个全局或局部错误,但我不明白。
def who_wins_when_player_3(player):
if player == 3:
amount_triangles = np.count_nonzero(board == 3)
if amount_triangles == 3 or 5 or 7:
player = 2
else:
player = 1
这里不行:
# vertical win check
for col in range(BOARD_COLS):
if board[0][col] == player and board[1][col] == player and board[2][col] == player or board[3][col] == player and board[1][col] == player and board[2][col] == player:
who_wins_when_player_3()
print(f"Player {player} wins")
return True
这里有效:
# vertical win check
for col in range(BOARD_COLS):
if board[0][col] == player and board[1][col] == player and board[2][col] == player or board[3][col] == player and board[1][col] == player and board[2][col] == player:
if player == 3:
amount_triangles = np.count_nonzero(board == 3)
if amount_triangles == 3 or 5 or 7:
player = 2
else:
player = 1
print(f"Player {player} wins")
return True
哪里出错了?
此致
在函数内部赋值不会做任何事情,除非您 return
该值。尝试此版本的函数,其中返回 player
的获胜值:
def who_wins_when_player_3(player):
if player == 3:
amount_triangles = np.count_nonzero(board == 3)
if amount_triangles == 3 or 5 or 7:
return 2
else:
return 1
return player # if we don't do this, the function will return None if player != 3
然后让调用者将其分配给自己范围内的player
:
# vertical win check
for col in range(BOARD_COLS):
if board[0][col] == player and board[1][col] == player and board[2][col] == player or board[3][col] == player and board[1][col] == player and board[2][col] == player:
player = who_wins_when_player_3(player) # pass player in and reassign it
print(f"Player {player} wins")
return True
此代码可能还有其他问题,但希望这至少可以阐明从函数返回值的工作原理。