我制作了一款基于文字的游戏,并且我正在使用字典来实现背包。当我尝试访问我在课堂上制作的商店时,会出现一个错误,即“#34; buy"和"出售"没有定义,它们是。在你找我之前,我已经向我提出过类似问题的每个查询。这是我的代码:
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
如果我选择&#34;出售&#34;,只是购买&#39;同样的事情就会发生。将被取代将&#39; sell&#39 ;. 请告诉我发生了什么以及如何解决它,我很困惑!!
答案 0 :(得分:3)
我认为你对课程的运作方式感到困惑。这是一个基本模板:
<强> backpack.py 强>
class Openshop:
def display(self, message):
print(message)
def buy(self):
self.display("You're buying!")
def sell(self):
self.display("Got something to sell?")
<强> main.py 强>
from backpack import Openshop
shop = Openshop()
shop.buy()
shop.sell()
以下是您可以使用此示例运行的一些实验:
self
。怎么了?为什么?Openshop.buy()
时会发生什么?为什么?一旦您对此示例感到满意,请继续并开始将现有逻辑移动到此代码中。
经常测试! (另外,使用REPL - 这是你的朋友!)
答案 1 :(得分:1)
由于您的buy()
方法是类openshop
的方法,因此您需要按如下方式调用它:
openshop.buy()
答案 2 :(得分:0)
在你的例子中,买是(试图 - 有其他问题,比如缺少自我论证)类openope的方法,所以你不得不说像openshop.buy(),或者
x = openshop()
x.buy()
但就像我说的那样,还有很多其他问题,代码不能运行这个修复程序。我建议从一些更小的例子开始,以便掌握使用Python类的编程。