0

I'm writing a text adventure game in Python and I'm curious on how I can go about listing all the items inside of the Room class

I'm very new to Python and have very limited practice.

# Declaring items and assigning them rooms

dingus = Item("Dingus", "This really dings.")
room["garden"].add_item(dingus)
flippers = Item("Flippers", "Webbed in nature.")
room["garden"].add_item(flippers)

# Declare all the rooms

room = {
    'garden':   Room("Garden",
                 """The flowers are blooming wonderfully. To the south lies a dark path.""")
}


class Room:

    def __init__(self, title, description):
        self.title = title
        self.description = description
        self.items = []
        self.items_in_room = ''

    def __repr__(self):
        print(f"-" * 40)
        return (f"You are at the {self.title}.")

    def add_item(self, item):
        return self.items.append(item)

    def list_items_in_room(self):
        for item in self.items:
            self.items_in_room += item
        ', '.split(self.items)
        return self.items


class Item:
    def __init__(self, name, description):
        self.name = name
        self.description = description

    def __str__(self):
        return f'{self.name} - {self.description}' + '\n' + "-" * 40

I'm expecting Room.list_items_in_room to list all the items in the room in a comma separated string.

kaiguy
  • 21
  • 4
  • 1
    Are you executing this code in the sequence you've shown here? I think you will have to define your classes first, then the room `room` dict and then the top code. – najeem Jan 13 '19 at 08:32
  • I am not, I just copied what I thought was important. I paid no regard to the order. I've been staring at this screen a bit too long it seems. – kaiguy Jan 13 '19 at 09:02

1 Answers1

1

I have re-arranged your code and also changed the function list_items_in_room. Also, have changed the __str__ function to __repr__ and removed the '-' * 40 (I couldn't understand why that's there).

class Room:
    def __init__(self, title, description):
        self.title = title
        self.description = description
        self.__items = []
        # __ so that it's not modifiable without getter and setter functions

    def __repr__(self):
        print(f"-" * 40)
        return (f"You are at the {self.title}.")

    def add_item(self, item):
        return self.__items.append(item)

    def list_items_in_room(self):
        return self.__items

class Item:
    def __init__(self, name, description):
        self.name = name
        self.description = description

    def __repr__(self):
        return f'{self.name} - {self.description}'


# Declare all the rooms
room = {
    'garden':   Room("Garden",
                 """The flowers are blooming wonderfully. To the south lies a dark path.""")
}

dingus = Item("Dingus", "This really dings.")
room["garden"].add_item(dingus)
flippers = Item("Flippers", "Webbed in nature.")
room["garden"].add_item(flippers)
print(room['garden'].list_items_in_room())

Output:

[Dingus - This really dings., Flippers - Webbed in nature.]
najeem
  • 1,841
  • 13
  • 29
  • That worked for me, thank you. I'm reading about getters and setters now. What exactly was wrong? Was it the __str__ method? – kaiguy Jan 13 '19 at 09:11
  • I won't know until you post what errors you got. One obvious thing would be the sequence in which you were executing the code blocks. The classes were not defined before you tried to created corresponding objects. Also, I couldn't understand why you were trying to create a string variable with a list of Items. I took liberty and changed it to *repr* and removed the string variable. – najeem Jan 13 '19 at 10:19
  • Here's a good explanation of *repr* vs *str* https://stackoverflow.com/a/2626364/3679377 – najeem Jan 13 '19 at 10:24