首页 > 解决方案 > Python 函数作用域

问题描述

我将播放器设置为“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)

标签: pythonfunctionscopes

解决方案


您正在打印相同的变量而不是打印函数。像这样编辑您的代码。

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

推荐阅读