Python 2.7 - 评估列表中的元素以在 for 循环中使用
Python 2.7 - Evaluate elements in a list to use in a for loop
嗨,我是 Python 的新手,我正在学习列表。
我正在编写一个小益智文字游戏,需要您使用基本命令从一个房间移动到另一个房间(功能到功能)。有些房间有陷阱,他们需要一个物品来移除陷阱。 E.G 房间里的熊要求你先找到蜂蜜,然后再给熊。我也了解如何 .remove() 和 .append() 列表。
我创建了 2 个列表:
inventory = ["Honey"]
trap = ["Bear"]
当我带着熊进入房间并拿到蜂蜜时,太棒了,我可以通过,但是我如何制作一个循环或#Something#来检查 'trap' 列表,以便 "bear" 元素不在列表中,他们不需要蜂蜜通过,因为很明显我在 'inventory' 列表中删除了 "honey" 他们在熊身上使用它。理论是,如果他们离开房间并想重新进入,他们就不再需要蜂蜜了。
我想我的理解是这样的,假设你已经找到了蜂蜜:
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()
对于此问题的任何建议或什至是不同的方法,我将不胜感激。
非常感谢!
使用 in
成员资格运算符确定元素是否在列表中。这不需要循环,它将测试每个元素。
if 'honey' in inventory and 'bear' not in trap:
room2()
elif ...
我无法得到完整的图片,但我希望你正在检查列表中是否存在某个项目。你可以简单地使用 "if & in"
if "bear" in trap: #use as required in your scenario
#do the required
也许我的回答是题外话,因为你只想学习列表,你谈论房间的功能,这里我用更复杂的列表做了一个简单的游戏(我希望能帮助你理解列表)收集关于 neightboors 房间和陷阱的信息。
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()
您已经完成了困难的部分,只需将您的英语翻译成 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()
嗨,我是 Python 的新手,我正在学习列表。
我正在编写一个小益智文字游戏,需要您使用基本命令从一个房间移动到另一个房间(功能到功能)。有些房间有陷阱,他们需要一个物品来移除陷阱。 E.G 房间里的熊要求你先找到蜂蜜,然后再给熊。我也了解如何 .remove() 和 .append() 列表。
我创建了 2 个列表:
inventory = ["Honey"]
trap = ["Bear"]
当我带着熊进入房间并拿到蜂蜜时,太棒了,我可以通过,但是我如何制作一个循环或#Something#来检查 'trap' 列表,以便 "bear" 元素不在列表中,他们不需要蜂蜜通过,因为很明显我在 'inventory' 列表中删除了 "honey" 他们在熊身上使用它。理论是,如果他们离开房间并想重新进入,他们就不再需要蜂蜜了。
我想我的理解是这样的,假设你已经找到了蜂蜜:
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()
对于此问题的任何建议或什至是不同的方法,我将不胜感激。 非常感谢!
使用 in
成员资格运算符确定元素是否在列表中。这不需要循环,它将测试每个元素。
if 'honey' in inventory and 'bear' not in trap:
room2()
elif ...
我无法得到完整的图片,但我希望你正在检查列表中是否存在某个项目。你可以简单地使用 "if & in"
if "bear" in trap: #use as required in your scenario
#do the required
也许我的回答是题外话,因为你只想学习列表,你谈论房间的功能,这里我用更复杂的列表做了一个简单的游戏(我希望能帮助你理解列表)收集关于 neightboors 房间和陷阱的信息。
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()
您已经完成了困难的部分,只需将您的英语翻译成 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()