CR0SS0V3R
CR0SS0V3R

Reputation: 336

Python object is in list yet isn't?

I am having an issue trying to make a small text-fantasy type game involving classes for each type of entity (a wall, the player, a book etc). I have a class called room that looks like this:

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

now I have two rooms that are defined like this (not saying that its right):

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)

now, the problem is this: when I run the program it works fine up until I try to take the candle from roomstart, it then spits out the error that the candle is not in a list:

(<type 'exceptions.ValueError'>, ValueError("'candle' is not in list",), <traceb
ack object at 0x00B8D648>)

(yes, I did use sys.exc_info())

each object, (the candle, a dagger, a robe etc) has a class as well:

class Object:

    def __init__(self, desc, worth, emitslight, readable, wearable, name):
        self.desc = desc
        self.worth = worth
        self.emitslight = emitslight
        self.readable = readable
        self.wearable = wearable
        self.name = name

here is the code for user input:

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

Upvotes: 1

Views: 127

Answers (1)

Aaron Dufour
Aaron Dufour

Reputation: 17505

The object candle is in the list, but the string 'candle' is not. You may wish to solve this with a dictionary of the objects:

objects = {}
objects['candle'] = candle
objects['robe'] = robe
...

Then, you can find the index of the item via

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

Upvotes: 5

Related Questions