对使用全局语句使用全局变量完全感到困惑
Completely confused on using global variables using global statement
下面这组代码完全依赖于全局变量:
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 时,它们在经过 'buying' 之后并没有改变。
我能否找到更好的方法来执行此操作或更好地使用 'global' 语句?
在您的代码中,似乎没有使用全局变量的具体原因:因为只有一个函数访问它们,所以它们最好在函数内部定义。
不过,我怀疑还有其他我们不知道的代码。
当您说变量的值没有改变时,最好显示您用来验证这一点的代码。很明显,在每个 运行 的开头,Gold
的值从 10.0 开始,并且应该在购买东西时发生变化。
如果有许多函数需要访问相同的状态变量,您很可能会发现将它们全部保留为某个状态对象的属性会更容易混淆,然后将其传递给每个函数以允许它访问并修改状态,现在可以在每个游戏功能中明确使用。例如,调用状态 'player',因为如果有多个玩家,似乎每个玩家都需要自己的状态,您可以这样写:
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
对象现在存储可以更改的各种内容。
通常不鼓励使用全局变量,因为它会使您的代码非模块化:函数需要的值应作为参数传递给方法调用,而不是保存在特定的全局变量中。
下面这组代码完全依赖于全局变量:
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 时,它们在经过 'buying' 之后并没有改变。 我能否找到更好的方法来执行此操作或更好地使用 'global' 语句?
在您的代码中,似乎没有使用全局变量的具体原因:因为只有一个函数访问它们,所以它们最好在函数内部定义。
不过,我怀疑还有其他我们不知道的代码。
当您说变量的值没有改变时,最好显示您用来验证这一点的代码。很明显,在每个 运行 的开头,Gold
的值从 10.0 开始,并且应该在购买东西时发生变化。
如果有许多函数需要访问相同的状态变量,您很可能会发现将它们全部保留为某个状态对象的属性会更容易混淆,然后将其传递给每个函数以允许它访问并修改状态,现在可以在每个游戏功能中明确使用。例如,调用状态 'player',因为如果有多个玩家,似乎每个玩家都需要自己的状态,您可以这样写:
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
对象现在存储可以更改的各种内容。
通常不鼓励使用全局变量,因为它会使您的代码非模块化:函数需要的值应作为参数传递给方法调用,而不是保存在特定的全局变量中。