正确删除列表中的内容

时间:2017-05-25 00:25:24

标签: python python-3.x

我目前正在制作一款Akinator类游戏,目前只有Zelda野生动物的呼吸。出于某种原因,当我输入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()

1 个答案:

答案 0 :(得分: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]