2014-04-10 113 views
0

我正在制作一个基于文本的游戏,并且我正在使用字典来实现一个背包。当我尝试访问我用班级制作的商店时,出现了一个错误,指出“购买”和“销售”未定义,它们是。在你去看我之前,我已经查看了所有对我有类似问题的查询。这里是我的代码: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 

同样的事情发生,如果我选择“卖”,只是'买'将被取代'卖'。 请告诉我发生了什么事以及如何解决它,我很困惑!

+0

“buy”是你的类的一个方法,要使用它,你必须实例化一个。 –

+0

从技术上讲,你只尝试过所有你能想到的。如果你已经尝试了一切,那么你会得到它的工作;) –

回答

1

由于您的buy()方法是类openshop的方法,你将需要调用它,如下所示:

openshop.buy()

+0

这是行不通的,因为'buy'没有'self'参数。 – Elektito

+0

@霍玛恩,对啊。我会删除它。 – sshashank124

+0

这是否工作?考虑到它不是@static_method –

0

在您的例子,买的是(想是 - 有喜欢的其他问题缺少自我参数)类openshop的方法,所以你必须要么说类似openshop.buy(),或

x = openshop() 
x.buy() 

但就像我说的,还有很多其他的问题和代码WIL我没有运行这个修复程序。我建议从一些小得多的例子开始,以便使用Python类进行编程。

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 - 这是你的朋友!)

相关问题