Python 函数范围

Python functions scopes

我已经将播放器设置为“x”,并定义了一个函数来反转播放器(您将在代码中看到它)。 有人可以帮我吗?

player = "x"

def update_player(player):
    if player == "x":
        player = "y"
    else:
        player = "x"
    return player

update_player(player) #called the function with argument player
print(player)

您正在打印相同的变量,而不是像这样打印 function.Edit 您的代码。

player = "x"
def update_player(player):
    if player == "x":
        player = "y"
    else:
        player = "x"
    return player
k=update_player(player)
print(k)