How do I keep a malleable instance variable/list called Inv while being able to add to it in several other classes?

Viewed 29

I am trying to create my first project using inheritance. My expectations were to use user input as a text adventure and collect items. While class StartEnviroment only has a sword in the Inventory list. there is a choice to puck up a rock as well. It returns Inventory which was to be expected. Once the user finds the choice to move North, South, East, or West, they would Type west and the code would break due to self.Inventory not being an attribute. But I still just want the list to be manipulated as you male the choices to pick up items.

Here is the code I've been using.

print("Text adventure attemp #8")

class Players:
    def __init__(self, Name, Inventory):
        self.Name = Name
        self.Inventory = Inventory

    def __str__(self):
        return (self.Name)


class Player1(Players):
    def __init__(self):
        self.Name = input("What's your name: ")
        self.Inventory = ["Sword"]

    def __str__(self):
        lirst = self.Name, self.Inventory
        return str(f"Hi {self.Name} your items {self.Inventory}")


class StartEnviroment(Player1):
    def __init__(self):
        self.Inventory = ["sword"]
        for i in self.Inventory:
            print("In a world of green, you see a small but odd rock... Would you like to pick up the rock?")
            inp = input().capitalize()
            if inp == "Y":
                print("ok")
                self.Inventory = ["sword", "Stone"]
            elif inp == "Yes":
              print("Yes")
              self.Inventory = ["sword", "Stone"]
            else:
              pass

    def __str__(self):
        return str(f"your items: {self.Inventory}")


class EastEnviroment(StartEnviroment):
  def __init__(self):
    self.Inventory = (f"{self.Inventory}")
    for i in self.Inventory:
         print("You end up in a forest where you find a twisting 6in stick. Do you pick it up?")
         inp = input().capitalize()
         if inp == "Y":
             print("ok")
             self.Inventory = ["sword", "Stone","Stick"]
         elif inp == "Yes":
           print("Yes")
           self.Inventory = ["sword", "Stone","Stick"]
         else:
           pass
           
  def __str__(self):
        return str(f"your items: {self.Inventory}")


def main():
    P1 = []
    World1 = []
    Group = Player1()
    P1.append(Group)
    WorldN = StartEnviroment()
    World1.append(WorldN)

    for i in P1:
        print(WorldN)

    print("would you go North, South, East, or West? ")
    Inp = input()
    if Inp == "East":
        return EastEnviroment()
    else:
      pass

main()
0 Answers
Related