我正在制作一个基于文本的游戏,并且我正在使用字典来实现一个背包。当我尝试访问我用班级制作的商店时,出现了一个错误,指出“购买”和“销售”未定义,它们是。在你去看我之前,我已经查看了所有对我有类似问题的查询。这里是我的代码:Python全局名称未定义错误,我试过了一切
class openshop():
def buy():
t_newcoincount = 0
shoploop = 0
print("Would you like to buy a potion of strength for 2 coins(1)\nOr a potion of health for 3 coins(2)?")
while shoploop == 0:
choice = input()
if choice == "1" and backpack["Coins"] >= 2:
t_newcoincount = backpack["Coins"]-2
backpack.update({"Coins":t_newcoincount})
if "Strength Potions" in backpack.keys():
t_spcount = backpack["Strength Potions"]+1
backpack["Strength Potions"] = t_spcount
else:
backpack["Strength Potions"] = 1
shoploop = 1
elif choice == "2" and backpack["Coins"] >= 3:
t_newcoincount = backpack["Coins"]-3
backpack.update({"Coins":t_newcoincount})
if "Health Potions" in backpack.keys():
t_hpcount = backpack["Health Potions"]+1
backpack["Health Potions"] = t_hpcount
else:
backpack["Health Potions"] = 1
shoploop = 1
elif choice == "1" and backpack["Coins"] < 2:
print("You cannot afford that")
menu()
elif choice == "2" and backpack["Coins"] < 3:
menu()
else:
print("PLEASE SELECT A VALID NUMBER")
menu()
def sell():
print("What would you like to sell?")
sellchoice = input()
if sellchoice == "bronze dagger":
t_newcoincount = backpack["Coins"]+5
backpack.update({"Coins":t_newcoincount})
if "Bronze Dagger" in backpack.keys():
if backpack["Bronze Dagger"] > 1:
t_bdcount = backpack["Bronze Dagger"]-1
backpack["Bronze Dagger"] = t_bdcount
else:
del backpack["Bronze Dagger"]
elif sellchoice == "leather boots":
t_newcoincount = backpack["Coins"]+5
backpack.update({"Coins":t_newcoincount})
if "Leather Boots" in backpack.keys():
if backpack["Leather Boots"] > 1:
t_bdcount = backpack["Leather Boots"]-1
backpack["Leather Boots"] = t_bdcount
else:
del backpack["Leather Boots"]
elif sellchoice == "health potion" or sellchoice == "health potions":
t_newcoincount = backpack["Coins"]+3
backpack.update({"Coins":t_newcoincount})
if "Health Potions" in backpack.keys():
if backpack["Health Potions"] > 1:
t_bdcount = backpack["Health Potions"]-1
backpack["Health Potions"] = t_bdcount
else:
del backpack["Health Potions"]
elif sellchoice == "strength potion" or sellchoice == "strength potions":
t_newcoincount = backpack["Coins"]+2
backpack.update({"Coins":t_newcoincount})
if "Strength Potions" in backpack.keys():
if backpack["Strength Potions"] > 1:
t_bdcount = backpack["Strength Potions"]-1
backpack["Health Potions"] = t_bdcount
else:
del backpack["Strength Potions"]
menu()
def menu():
for keys,values in (backpack).items():
print("You have",values,keys)
print("Would you like to buy(1) or sell(2)?")
dec = input()
if dec == "1" or dec == "buy":
buy()
elif dec == "2" or dec == "sell":
sell()
menu()
,这里是我以前从一个不同的文件(同一文件夹中明显)访问它的两条线:
import Backpack
backpack.openshop()
,这里是错误消息我得到:
Traceback (most recent call last):
File "C:\Documents and Settings\user\Desktop\Text Based Game\Backpack import test.py", line 1, in <module>
import Backpack
File "C:\Documents and Settings\user\Desktop\Text Based Game\Backpack.py", line 11, in <module>
class openshop():
File "C:\Documents and Settings\user\Desktop\Text Based Game\Backpack.py", line 110, in openshop
menu()
File "C:\Documents and Settings\user\Desktop\Text Based Game\Backpack.py", line 105, in menu
buy()
NameError: global name 'buy' is not defined
同样的事情发生,如果我选择“卖”,只是'买'将被取代'卖'。 请告诉我发生了什么事以及如何解决它,我很困惑!
“buy”是你的类的一个方法,要使用它,你必须实例化一个。 –
从技术上讲,你只尝试过所有你能想到的。如果你已经尝试了一切,那么你会得到它的工作;) –