【问题标题】:Python 2.7 - Evaluate elements in a list to use in a for loopPython 2.7 - 评估列表中的元素以在 for 循环中使用
【发布时间】:2015-05-26 10:55:35
【问题描述】:

您好,我是 Python 新手,正在学习列表。

我正在编写一个小益智文字游戏,需要您使用基本命令从一个房间移动到另一个房间(功能到功能)。有些房间有陷阱,他们需要一个物品来移除陷阱。 E.G 房间里的熊要求你先找到蜂蜜,然后把它交给熊。我也了解如何 .remove() 和 .append() 列表。

我创建了 2 个列表:

inventory = ["Honey"]
trap = ["Bear"]

当我和熊一起进入房间并且我有蜂蜜时,我可以通过,但是我如何制作一个循环或#Something#来检查“陷阱”列表,以便如果“熊”元素不是t 在列表中,他们不需要蜂蜜通过,因为很明显,一旦他们在熊身上使用它,我就会从“库存”列表中删除“蜂蜜”。理论上,如果他们离开房间并想重新进入,他们就不再需要蜂蜜了。

我想我对这个的理解是这样的,也假设你已经找到了蜂蜜:

def room1()
    print "You are in ROOM 1"

    if #item is not in list# and #bear is not in the room#:
        room2()
    elif #item is in the bag#:
        print "You give the honey to the bear, and it is distracted"
        inventory.remove("Honey")
        trap.remove("Bear")
        room2()
    else:
        print "You die to the bear"
        exit()

我真的很感激任何建议,甚至是解决这个问题的不同方法。 非常感谢!

【问题讨论】:

  • if 'Bear' in trap and 'Honey' in inventory:?

标签: python list python-2.7 loops evaluate


【解决方案1】:

使用in 成员运算符来确定元素是否在列表中。这不需要循环,它将测试每个元素。

if 'honey' in inventory and 'bear' not in trap:
    room2()
elif ...

【讨论】:

    【解决方案2】:

    我无法获得完整的图片,但我希望您正在寻找一个项目是否存在于列表中。你可以简单地使用"if & in"

    if "bear" in trap: #use as required in your scenario
        #do the required
    

    【讨论】:

      【解决方案3】:

      也许我的回答超出了主题,因为您只想学习列表并且您将房间作为功能来讨论,这里我使用更复杂的列表制作了一个简单的游戏(我希望这有助于您理解列表) 收集有关邻居房间和陷阱的信息。

      class puzzle:
      def __init__(self):
          self.lives = 1
          self.inventory = ['honey']
          self.trap_object = {'bear': 'honey', 'fire': 'water'}
          self.rooms = [
              ['room 0', {'trap': None, 'doors': [1,2,3]}],
              ['room 1', {'trap': 'bear', 'doors': [0,3]}],
              ['room 2', {'trap': 'fire', 'doors': [0,4]}],
              ['room 3', {'trap': None, 'doors': [0,1,5]}],
              ['room 4', {'trap': None, 'doors': [2,6]}],
              ['room 5', {'trap': 'bear', 'doors': [3]}],
              ['room 6', {'trap': None, 'doors': [4]}]
          ]
      
      def gameLoop(self):
          actual_room = self.rooms[0]
          while(self.lives > 0):
              print "You are in room %s.\n" % (actual_room[0])
              n_doors = len(actual_room[1]['doors'])
              print "You have %d doors:.\n" % (n_doors)
              for n_door in actual_room[1]['doors']:
                  print "Room number %d\n" % (n_door)
      
              valid = False
              room = None
              while not valid:
                  room  = int(raw_input('Select a room:\n'))
                  if room in actual_room[1]['doors']:
                      valid = True
      
              print "You enter in room %d..." % (room)
              actual_room = self.rooms[room]
      
              if actual_room[1]['trap'] == None:
                  print "The room seems to be safe!\n"
              else:
                  trap = actual_room[1]['trap']
                  useful_obj = self.trap_object[trap]
                  print "There is a trap in this room!: %s\n" % (trap)
                  if useful_obj in self.inventory:
                      print "You had an object to avoid the trap :)\n"
                      self.inventory.remove(useful_obj)
                  else:
                      print "You had nothing to avoid the trap :(\n"
                      self.lives -= 1
      
      
      
      game = puzzle()
      game.gameLoop()
      

      【讨论】:

      • 哇!这看起来非常棒!非常感谢您经历了这一切。虽然一半没看懂哈哈,我现在才开始上课。在函数中还有一些我不明白的东西。但这不是你的错,只是我缺乏理解atm对不起。我是全新的。再次感谢!
      • 我想我会哈哈。有没有可以用来单步执行代码的程序?
      【解决方案4】:

      你已经完成了困难的部分,只需用 Python 翻译你的英语:

      if #item is not in list# and #bear is not in the room#:
      

      变成:

      if 'Honey' not in inventory and 'Bear' not in trap:
      

      此外,我会先检查熊是否在场,然后再检查是否有蜂蜜:

      def room1()
          print "You are in ROOM 1"
          if 'Bear' not in trap:
              room2()
          else:
              if 'Honey' in inventory:
                  print "You give the honey to the bear, and it is distracted"
                  inventory.remove("Honey")
                  trap.remove("Bear")
                  room2()
              else:
                  print "You die to the bear"
                  exit()
      

      【讨论】:

      • 太完美了!谢谢!是的,我同意我会先给熊打电话。
      猜你喜欢
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      相关资源
      最近更新 更多