2016-08-04 112 views
0

我的程序是一個冒險遊戲藥水和箱子。我將這些設置在列表中,因此當用戶喝了一劑藥水或打開一個箱子時,我可以使用pop()將它從列表中刪除。當用戶在同一個房間裏時,這可以正常工作,但是如果他們回到房間,他們可以再次使用該藥水或胸部。列表和流行()不工作,因爲我期望

僞代碼,系統應工作是這樣的:

if potions: 
    if there are still potions in the list: 
     drink potion 
     potions.pop() 
    else: 
     print "There are no potions left." 
else: 
    print "There are no potions to drink." 

我認爲正在發生的事情是,當我從列表彈出藥水,if potions不計算到爲真,它會自動進入到其他區塊,但我不確定它爲什麼這樣做,我也不確定爲什麼當我回到房間時,列表會自動重置。

這很可能是因爲我沒有完全理解列表或pop方法是如何工作的,所以如果有人能夠澄清那會很好。 謝謝!

編輯真正的代碼從OP的鏈接

if room.potions: 
    if len(room.potions) > 0: # if there are potions left 
     if hasattr(room.potions[0], 'explode'): # if the potion is a bomb 
      room.potions[0].explode(You) # explode using zeroeth item from the list 
      room.potions[0].pop # remove zeroeth item from the list` 
     else: 
      room.potions[0].heal(You) # heal using zeroeth potion from the list 
      room.potions.pop(0) # remove zeroeth item from the list 
    else: 
     print "There are no more potions to drink" 
else: 
    print "There are no potions to drink" 

編輯:解決

我的問題是,當我把在詞典中的參數,我把整個房間作爲一個他們,認爲我能夠使用它的一切都很好。但是,每次我這樣做,它都會將空間設置爲init,從而有效地破壞我正在嘗試做的事情。我爲魔藥和胸部添加了單獨的參數,現在它完美地工作。 謝謝@Bernie提供的有用建議可以讓我看到錯誤。 也謝謝你@你的建議和提示。 ,

if potions: # same as len(potions) > 0 
    # drink potion 
    potions.pop() 
else: 
    print "There are no potions to drink." 

所以,檢查是否有任何藥水留如果是左圖:

+4

你可以發佈代碼給你一個錯誤嗎? –

+0

我會放一個魔藥部分的鏈接。爆炸部分不是我所擔心的。 [鏈接](https://www.dropbox.com/s/pecf9cyv2n45owt/testfile.py?dl=0) –

+0

你也應該擔心爆炸部分,因爲它說藥劑[0] .pop()而不是藥劑。流行(0)。 –

回答

1

你可以做的,當他們喝它做到這一點

potions = 1 
    if potions > 0: 
     drink potion 
     potions = potions - 1 
    else: 
     print "There are no potions left." 
+0

第二個是什麼? –

+0

錯字我從腳本中拷貝了腳本,現在修復它。 – thesonyman101

+0

如果有多個魔藥會怎麼樣? –

0

你可以簡單地編寫代碼如下喝它,從列表中刪除,否則顯示沒有藥水。

+0

這有幫助。我想我只是試圖過度設計這個問題。唯一需要弄清楚的是爲什麼它不斷重置列表。該列表是在房間的__init__函數中創建的。我建立它的方式不應該繼續創建列表,它應該只做一次,對吧? –

+0

@BACeradsky我認爲你很可能遇到了這個問題:http://docs.python-guide.org/en/latest/writing/gotchas/ – bernie

+0

@Bernie謝謝你的文檔,但我想我找到了問題。我的程序相當大,所以我一直試圖將某些東西分成模塊並導入它們。主要的是各種詞典,處理用戶輸入的東西。問題是它只返回一個值,即用戶想要去的下一個房間的數量。所以也許因爲它只返回那個值,它忘記了我所做的所有彈出? –

0

EAFP方法:

try: 
    potions.pop() 
except IndexError: 
    print "No potion left" 

更容易請求原諒比許可。這種常見的Python編碼風格假定存在有效的鍵或屬性,並且如果假設證明爲假,則捕獲異常。這種乾淨而快速的風格的特點是存在很多嘗試和除了 陳述。該技術與常見的許多 其他語言LBYL風格如C.

0

OK對比。我被誘惑...... :)當然,應該存在一個藥水層次,並且這個方法可能會成爲'世界'的信息。等等......

import random 

class Potion: 
    """Define the Potion class. Could be explosive and provoke a damage""" 
    def __init__(self, explosive=None, damage=None): 
     if explosive == None: 
      self.__explosive = bool(random.getrandbits(1)) 
     else: 
      self.__explosive = explosive 

     if self.__explosive: 
      if damage == None: 
       self.__damage = random.randint(1,5) 
      else: 
       self.__damage = damage 
     else: 
      self.__damage = 0 

    def isExplosive(self): 
     return self.__explosive 

    def explode(self): 
     if self.isExplosive(): 
      return self.__damage 
     else: 
      return 0 

    def __str__(self): 
     if self.isExplosive(): 
      res = "explosive potion" 
     else: 
      res = "simple potion" 
     return res 

class Hero: 
    """Simple Hero class""" 
    def __init__(self, name): 
     self.__name = name 
     self.__hp = 100 
     self.__potions = [] 

    def receiveDamage(self, damage): 
     self.__hp = self.__hp - damage 
     if self.__hp <= 0: 
      print("I'm dead!") 

    def isAlive(self): 
     return self.__hp > 0 

    def getPotion(self, room): 
     """ Get the first potion in the room.If the potion 
      is explosive, explode and receive some damage 
      TODO: Trapped potions? 
     """ 
     potion = room.getFirstPotion() 
     print("%s take a %s" % (self.__name, potion)) 
     if potion.isExplosive(): 
      damage = potion.explode() 
      print("%s received %d hp of damage!!" % (self.__name, damage)) 
      self.receiveDamage(damage) 
     else: 
      self.__potions.append(potion) 

    def __str__(self): 
     res = "%s have %d HP and %d potions." % (self.__name, self.__hp, len(self.__potions)) 
     return res 

class Room: 
    def __init__(self, potions): 
     """Create a room with some potions""" 
     self.__potions = potions 

    def getFirstPotion(self): 
     """Return the first potion """ 
     if self.__potions: 
      potion = self.__potions.pop() 
     else: 
      potion = None 
     return potion 

    def havePotions(self): 
     return len(self.__potions) > 0 

    @property 
    def potions(self): 
     return self.__potions 

    def __str__(self): 
     return "This room have %s potions" % len(self.__potions) 

peter = Hero('Peter') 
room = Room(potions=[Potion() for i in range(5)]) 
print(room) 
print(peter) 

while room.havePotions(): 
    print("___________") 
    peter.getPotion(room) 
    print(room) 
    print(peter) 

輸出:

這個房間有5個藥水
Peter有100 HP和0藥水。
___________
彼得拿一個簡單的藥水
這個房間有4個藥水
彼得有100個HP和1個藥水。
___________
彼得拿起炸藥
彼得得到3點傷害!!
這個房間有3個藥水
Peter有97 HP和1 藥水。
___________
彼得拿起炸藥
彼得得到3點傷害!!
這個房間有2個藥水
Peter有94 HP和1 魔藥。
___________
彼得拿着一個爆炸藥水
彼得得到5馬力的傷害!
這個房間有1個藥水
Peter有89 HP和1 魔藥。
___________
彼得拿起炸藥
彼得得到1點傷害!!
這個房間有0藥水
彼得有88 HP和1 藥水。

相關問題