Python对象是否在列表中?

Python对象是否在列表中?,python,Python,我在尝试制作一个小的文本幻想类型的游戏时遇到了一个问题,涉及到每种类型的实体(墙、玩家、书等)的类。我有一个名为room的类,看起来像这样: class Room: def __init__(self, desc, items, wallF, wallB, wallL, wallR, isdark): self.desc = desc self.items = items self.wallF = wallF self.w

我在尝试制作一个小的文本幻想类型的游戏时遇到了一个问题,涉及到每种类型的实体(墙、玩家、书等)的类。我有一个名为room的类,看起来像这样:

class Room:

    def __init__(self, desc, items, wallF, wallB, wallL, wallR, isdark):
        self.desc = desc
        self.items = items
        self.wallF = wallF
        self.wallB = wallB
        self.wallL = wallL
        self.wallR = wallR
        self.isdark = False
现在我有两个房间的定义是这样的,并不是说它是对的:

roomstart = Room('There is a hole in the ceiling where you seemed to have fallen through, there is no way back up...', [candle], True, False, False, False, False)
room2 = Room('You enter a small cobblestone cavort. It is dark, and the smell of rot pervades you', [spellbook], False, True, False, True, True)
现在,问题是:当我运行该程序时,它工作正常,直到我尝试从roomstart获取蜡烛,然后它抛出蜡烛不在列表中的错误:

(<type 'exceptions.ValueError'>, ValueError("'candle' is not in list",), <traceb
ack object at 0x00B8D648>)
以下是用户输入的代码:

def handleinput():
global moves
room = Player.location
if room.isdark == True:
    print 'It is rather dark in here...'
else:
    print room.desc, 'You see here:',
    for i in room.items:
        print i.name
input = str(raw_input('What now? ')).lower()
if 'look' in input:
    if room.isdark==True:
        print "You can't see anything! Its too dark."
    else:
        print 'You see:',room.desc, room.items.name
        if room.wallF == True:
            print 'There is an exit to the front.'
        elif room.wallB == True:
            print 'There is an exit behind you.'
        elif room.wallL == True:
            print 'There is an exit to your left.'
        elif room.wallR == True:
            print 'There is an exit to your right.'

elif 'grab' in input:
    if room.isdark==True:
        print 'You flail about blindly in the dark room!'
    else:
        input2 = str(raw_input('Take what? '))
        try:
            popp = room.items.index(input2)
            print popp
        except:
            print sys.exc_info()
            print input2.title(),"doesn't exist!"
        else:
            print "You take the",input2,"and store it in your knapsack."
            room.items.pop(popp)
            Player.inventory.append(input2)

elif 'wipe face' in input:
    os.system('cls')
moves += 1
对象蜡烛在列表中,但字符串“蜡烛”不在列表中。您可能希望使用对象字典来解决此问题:

objects = {}
objects['candle'] = candle
objects['robe'] = robe
...
然后,您可以通过

popp = room.items.index(objects[input2])
对象蜡烛在列表中,但字符串“蜡烛”不在列表中。您可能希望使用对象字典来解决此问题:

objects = {}
objects['candle'] = candle
objects['robe'] = robe
...
然后,您可以通过

popp = room.items.index(objects[input2])

好吧,至少我们不能抱怨他没有展示他迄今为止的尝试。你知道蜡烛和“蜡烛”是不同的对象,对吧?你给我们展示代码真是太好了,真的,这很有帮助。更有用的是,您可以尝试找到能够证明您的问题的最小代码片段。这对你很有帮助。@gnibbler如果你在蜡烛对象构造的末尾谈到“蜡烛”,那么是的,“蜡烛”就是对象。我用来为ingame taggingWell命名蜡烛的名称字符串。至少我们不能抱怨他没有展示他迄今为止所做的尝试。你知道蜡烛和“蜡烛”是不同的对象,对吧?你给我们看代码真是太好了,真的,这很有帮助。更有用的是,您可以尝试找到能够证明您的问题的最小代码片段。这会对你很有帮助。@gnibbler如果你在蜡烛对象构造的末尾谈论“蜡烛”,那么“蜡烛”就是对象。我用来为ingame标记的蜡烛命名的名称字符串哇!那很有效!只需记住将对象添加到列表中;再次感谢!成功的甜言蜜语:天花板上有一个洞,你似乎已经跌破了,没有办法回头了。。。你看这里:蜡烛现在怎么了?拿什么?蜡烛你把蜡烛放在背包里。天花板上有一个洞,你好像从那里掉下来了,没有办法再爬起来。。。你看:现在怎么办?哇!那很有效!只需记住将对象添加到列表中;再次感谢!成功的甜言蜜语:天花板上有一个洞,你似乎已经跌破了,没有办法回头了。。。你看这里:蜡烛现在怎么了?拿什么?蜡烛你把蜡烛放在背包里。天花板上有一个洞,你好像从那里掉下来了,没有办法再爬起来。。。你看,现在怎么办?