完全困惑于使用全局语句使用全局变量

时间:2018-12-19 16:08:06

标签: python global

以下代码集完全依赖于全局变量:

SwordImpact = 1
SwordCrit = 0
SwordSwingSpeed = 2
SDCost = 1.99
SCCost = 4.99
SSSCost = 2.99
Gold = 10.0
inventory = []
Location = 'Town Center'


def Shop():
    global Gold
    global inventory
    global SDCost
    global SCCost
    global SSSCost
    global SwordImpact
    global SwordCrit
    global SwordSwingSpeed
    if Location == 'Town Center':
        Buy = input('Catalog:\nA - Health potion (5.00)\nB - Apple (1.00)\nC - Upgrade Sword (Varies)\nD - Regen potion (7.00)\n---|')
        if Buy == 'A':
            if Gold > 4.99:
                Gold = Gold - 4.99
                inventory.append('Health potion')
                print('Item bought.')
            else:
                print('Not Enough Gold.')
        if Buy == 'B':
            if Gold > 0.99:
                Gold = Gold - 0.99
                inventory.append('Apple')
                print('Item bought.')
            else:
                print('Not Enough Gold.')
        if Buy == 'C':
            Upgrade = input('Select Upgrade:\nA - Sword Damage (2.00 - (increases by 2 each upgrade))\nB - SwordCrit (5.00 (increases by 3 each upgrade))\nC - Sword Swing Speed (3.00 (Increases by 3 each upgrade))\n---|')
            if Upgrade == 'A':
                verify = input('Are you sure you want to pay ' + str(SDCost) + ' for this upgrade?\nA - Yes\nAnything else - No\n---|')
                if verify == 'A':
                    if int(Gold) > int(SDCost):
                        Gold = int(Gold) - int(SDCost)
                        SDCost = int(SDCost) + 2
                        SwordImpact = SwordImpact + 1
                    else:
                        print('Not enough gold.')
            if Upgrade == 'B':
                verify = input('Are you sure you want to pay ' + str(SCCost) + ' for this upgrade?\nA - Yes\nAnything else - No\n---|')
                if verify == 'A':
                    if int(Gold) > int(SCCost):
                        Gold = int(Gold) - int(SCCost)
                        SCCost = int(SCCost) + 3
                        SwordCrit = SwordCrit + 1.5
                    else:
                        print('Not enough gold.')
            if Upgrade == 'C':
                verify = input('Are you sure you want to pay ' + str(SSSCost) + ' for this upgrade?\nA - Yes\nAnything else - No\n---|')
                if verify == 'A':
                    if int(Gold) > int(SSSCost):
                        Gold = int(Gold) - int(SSSCost)
                        SSSCost = int(SSSCost) + 3
                        SwordSwingSpeed = SwordSwingSpeed + 1.0
                    else:
                        print('Not enough gold.')
        if Buy == 'D':
            if Gold > 6.99:
                Gold = Gold - 6.99
                inventory.append('Regen potion')
                print('Item bought.')
            else:
                print('Not Enough Gold.')
    else:
        print('You need to be in Town Center.')

但是我对这套代码有一个主要问题:当我检查变量Gold和Inventory时,它们在经过“购买”之后并没有改变。 我能否获得更好的方法或更好地使用'global'语句?

1 个答案:

答案 0 :(得分:1)

在您的代码中,似乎没有使用全局变量的特定原因:由于恰好有一个函数可以访问它们,因此也应该在函数内部定义它们。

但是,我怀疑还有其他我们不知道的代码。

当您说变量的值没有改变时,最好显示您用来验证的代码。显然,在每次运行开始时,Gold的值都从10.0开始,并且在购买商品时应该会改变。

如果有许多函数需要访问相同的状态变量,则很可能会发现将它们全部保留为某个状态对象的属性会减少混乱,然后将其传递给每个函数以允许其访问和修改状态,现在可以在每个游戏功能中明确使用该状态。例如,将状态称为“玩家”,因为如果有多个玩家,似乎每个玩家都需要自己的玩家,因此您可以这样写:

class Player:
    def __init__(self):
        self.SwordImpact = 1
        self.SwordCrit = 0
        self.SwordSwingSpeed = 2
        self.SDCost = 1.99
        self.SCCost = 4.99
        self.SSSCost = 2.99
        self.Gold = 10.0
        self.inventory = []
        self.Location = 'Town Center'

您的代码将随后显示:

def Shop(player):
    if player.Location == 'Town Center':
        Buy = input('Catalog:\nA - Health potion (5.00)\nB - Apple (1.00)\nC - Upgrade Sword (Varies)\nD - Regen potion (7.00)\n---|')
        if Buy == 'A':
            if player.Gold > 4.99:
                player.Gold -=  4.99
                player.inventory.append('Health potion')
                print('Item bought.')
            else:
                print('Not Enough Gold.')

依此类推-请注意,player对象现在存储了可以更改的各种内容。

通常不建议使用全局变量,因为它会使您的代码成为非模块化的:函数所需的值应作为参数传递给方法调用,而不是保留在特定的全局变量中。