Python:如何从模块中的函数内修改外部变量?

问题描述 投票:0回答:1

如何从函数内更新

player_points
的值?

我有一个具有此功能的模块。我想更改函数外部的变量

player_points
。我该怎么做?

此外,

player_points
只能从模块内部访问。

player_points=0 # how to update this var from within the function?

def player_draws_card():

    drawn_card = random.choice(current_deck)
    value = current_deck.pop(drawn_card)
    player_hand[drawn_card] = value
    
    player_points += value # this is the part that doesn't work

    return drawn_card
python python-3.x function variables scope
1个回答
0
投票

您想在函数开始时声明“全局玩家点”。

© www.soinside.com 2019 - 2024. All rights reserved.