【问题标题】:Python functions scopes [duplicate]Python函数范围[重复]
【发布时间】:2020-12-28 15:24:02
【问题描述】:

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

【问题讨论】:

  • 您需要分配回报 - player = update_player(player)。或者您可以将 player 用作全局变量而不是参数。
  • 谢谢,它帮助了我。如果您将其写为答案,我将用绿色“v”标记。非常感谢!

标签: python function scopes


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 2011-08-22
    • 2016-07-14
    • 2011-11-19
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多