2017-05-25 47 views
-4

我目前正在製作一款類似Akinator的遊戲,該遊戲目前只支持塞爾達野生物種的呼吸。出於某種原因,當我輸入coocoo的所有信息時,輸出是Gerudo。請幫忙。下面是代碼:正確地從列表中刪除東西

class Thing(): # The general class for a object 
    def __init__(self, area, race, ally, living, extra): 
    self.area = area 
    self.race = race 
    self.ally = ally 
    self.living = living 
    self.extra = extra 
class Guess(): # The class for the guess 
    def __init__(self): 
    self.area = None 
    self.race = None 
    self.ally = bool 
    self.living = bool 
    self.extra = str 
talus = Thing("everywhere", "talus", False, True, " rocky") 
coocoo = Thing("everywhere", "coocoo", True, True, " a fierce, chicken-like friend") 
zora = Thing("in the zora area", "zora", True, True, " swimming") 
rito = Thing("in the rito area", "rito", True, True, " flying") 
goron = Thing("in the goron area", "goron", True, True, " rolling") 
gerudo = Thing("in the gerudo area", "gerudo", True, True, " a women") 
hylean = Thing("everywhere", "hylean", True, True, " good and bad") 
guardian = Thing("everywhere", "guardian", False, False, " mechanical") 
moblin = Thing("everywhere", "moblin", False, True, " related to the bokoblin") 
staloblin = Thing("everywhere", "staloblin", False, False, " a large undead enemy") 
bokoblin = Thing("everywhere", "bokoblin", False, True, " a basic enemy") 
stalkoblin = Thing("everywhere", "stalkoblin", False, False, " a basic undead enemy") 
lynel = Thing("everywhere", "lynel", False, True, " a horse-like beast") 
octorok = Thing("everywhere", "octorok", False, True, " a rock-shooting monster") 
lizafos = Thing("everywhere", "lizafos", False, True, " a scaley, speeding, baddie") 
stalzafos = Thing("everywhere", "stalzafos", False, False, " a fast, skeletal enemy") 
spirit = Thing("everywhere", "spirit", True, False, " a helpful ghost") 
def akinator(): 
    everything = [talus, zora, rito, goron, gerudo, hylean, guardian, moblin, stalkoblin, staloblin, bokoblin, lynel, octorok, lizafos, stalzafos, spirit, coocoo] 
    possible_areas = ["everywhere", "in the zora area", "in the rito area", "in the goron area", "in the gerudo area", "in the hylean greenlands"] 
    guess = Guess() 
    alive = input("Is it alive? y/n ") 
    if alive == "n": 
    guess.living = False 
    elif alive == "y": 
    guess.living = True 
    for i in everything: 
     if i.living != guess.living: 
      everything.pop(everything.index(i)) 
    ally = input("Is it one of your allies? y/n ") 
    if ally == "n": 
    guess.ally = False 
    elif ally == "y": 
    guess.ally = True 
    for i in everything: 
     if i.ally != guess.ally: 
      everything.pop(everything.index(i)) 
    for i in possible_areas: 
     area = input("Do they live " + i + "? y/n ") 
     if area == "y": 
      guess.area = i 
      break 
    for i in everything: 
     if i.area != guess.area: 
      everything.pop(everything.index(i)) 
    for i in everything: 
     extra = input("Is it" + i.extra + "? y/n ") 
     if extra == "n": 
     everything.pop(everything.index(i)) 
     if extra == "y": 
     print("Is it a "+ i.race +"?") 
     quit() 
    print("Is it a "+everything[0].race+"?") 

akinator() 
+0

你所要的輸出是什麼? –

+0

你能顯示你的輸入和輸出嗎 – Ouss

+0

請閱讀這個[怎麼問](http://stackoverflow.com/help/how-to-ask)並遵循那裏的指導來改善你的問題,並提供足夠的信息描述並重現你的問題。 – thewaywewere

回答

1

從列表膨化項目,而你迭代這是一個壞主意 - 它弄壞你的列表迭代器,讓你跳過測試以下項目。

而不是

for i in everything: 
    if i.living != guess.living: 
     everything.pop(everything.index(i)) # <- BAD 

嘗試

everything = [i for i in everything if i.living == guess.living] 
+0

工作。謝謝 –

相關問題