pibbles97
pibbles97

Reputation: 23

How to append an instance of a class and then pull out an attribute of an instance in Python?

I am working on creating a Text Adventure Game.

I have created a class and made several different instances of the class. The class has many attributes. I am trying to append each instance of the class to a list. I then want to be able to pull out one attribute from one instance inside of the list.

I was told to implement:

"Rather than have each room be a list of [description, north, east, south, west] create a Room class. The class should have a constructor that takes in (description, north, east, south, west) and sets fields for the description and all of the directions. Get the program working with the new class. "

Here is the code I have so far:

room_list = []

class Room():
    def __init__(self, describe, nw, n, ne, e, se, s, sw, w):
        self.description = describe
        self.northwest = nw
        self.north = n
        self.northeast = ne
        self.east = e
        self.southeast = se
        self.south = s
        self.southwest = sw
        self.west = w

kitchen = Room("You are in the Kitchen. Look at the scrumptious roast chicken and kidney pudding! \nThere are doors leading to North, East, and West.", None, 4, None, 2, None, None, None, 0)

room_list.append(kitchen)

east_cooridor = Room("You apparated into the East Corridor. \nYou can apparate to the Northwest or the Southwest.", 8, None, None, None, None, None, 2, None)

room_list.append(east_cooridor)

great_hall = Room("You are in the Great Hall. What is that great smell? \nThere appears to be doors leading to the north and the south.", None, 7, None, None, None, 1, None, None)

room_list.append(great_hall)

west_cooridor = Room("You apparated into the West Corridor. \nYou can apparate to the Northeast or the Southeast.", None, None, 6, None, 0, None, None, None)

room_list.append(west_cooridor)

owlery = Room("You are in the Owlery. WHOOs got mail? There is a glass door overlooking the Forbidden Forest. \nThere are doors in every direction.", None, 9, None, 8, None, 4, None, 6)

room_list.append(owlery)

forbidden_forest = Room("Yikes! You are in the Forbidden Forest! Is that a dead unicorn? ...Or is it Aragog? \nGet back into the Owlery, quick! \nOf course...if you wanted to explore the forest more, you could certainly try!", None, None, None, None, None, 7, None, None)

room_list.append(forbidden_forest)

current_room = 4
while done == False:
    print(room_list[current_room][0])

The last line of code errors out and says: builtins.TypeError: 'Room' object does not support indexing

Instead, I want it to pull out the phrase "You are in the Owlery. WHOOs got mail? There is a glass door overlooking the Forbidden Forest. There are doors in every direction" from the owlery room.

What I have tried so far has not worked. I would appreciate any advice possible. Thank you!

Upvotes: 1

Views: 51

Answers (1)

Sounds like you don't really understand how attributes works. The attributes in a class can't usually be accessed as an array. If you want to print, say, the description of a room, you should do it calling explicitly the attribute:

current_room = 4
while not done:  # In python, "not done" is preferred over "done == False"
    print(room_list[current_room].description)

Upvotes: 4

Related Questions