0

I'm running this code and when i add an item to the inventory,the item appears in all the Inventory Classes, Where am I going wrong here? Once i run Browse(),add new inventory,add items to it, and when i make a new inventory, items from the first inventory appear in the new one by default. how can i fix this problem boys and girls?

inventories = {}

class Inventory: ##Class for new inventory

    inventory = {}
    items = {}

    def __init__(self,name):
        self.name = name


    def add_item(self,item,price,amount):
        if item in self.items:
            return False
        else:
            self.items[item] = int(price)
            self.inventory[item] = int(amount)
            return True

    def add_amount(self,item,amount):
        self.inventory[item] += amount

    def update_amount(self,item,amount):
        self.inventory[item] = amount

    def remove_amount(self,item,amount):
        self.inventory[item] -= amount

    def update_item_price(self,item,price):
        self.items[item] = price

    def inv_list(self): ##Print the class
        print(self.name)
        print("\nItem:          Price:        Amount:\n")
        for item in self.inventory:
            print(f"{item}\t\t{self.items[item]}\t\t{self.inventory[item]}")



def new_inventory(): ##MAkes a new inventory class and stores in inventories{}
    global inventories
    while True:
        answer = input("What would you like to call your new Inventory?")
        if answer in inventories:
            print("Name already taken")
            continue

        inventories[answer] = Inventory(answer)
        print("Inventory created")
        update(answer)
        break



def update(inv): ##Updates an excisting Inventory
    global inventories
    while True:
        inventories[inv].inv_list()
        answer = int(input("1.Add item to inventory\n2.Update items price\n3.Update items amount\n4.Exit"))
        if answer == 1:
            while True:
                name = input("Name of item")
                price = int(input("Price"))
                amount = int(input("Amount"))
                answer = inventories[inv].add_item(name,price,amount)
                if answer == False:
                    print("Item already in inventory")
                    continue
                break
        if answer == 2:
            while True:
                print(inventories[inv].inv_list())
                name = input("item")
                price = int(input("Price"))
                if name in inventories[inv].items:
                    inventories[inv].update_item_price(name,price)
                    break
                print("No such item")
                continue
        if answer == 3:
            while True:
                print(inventories[inv].inv_list())
                name = input("item")
                amount = int(input("Amount"))
                if name in inventories[inv].items:
                    inventories[inv].update_amount(name,amount)
                    break
                print("No such item")
                continue
        if answer == 4:
            break




def browse(): ##Runs the prigram Function
    global inventories
    while True:
        print("Welcome to your inventory collection\nWhat action would you like to take?")
        print("1.Add a new inventory list\n2.Update excisting inventory\n3.Exit")
        answer = int(input())
        if answer == 1:
            new_inventory()
        if answer == 2:
            for x in inventories:
                print(x)
            while True:    
                answer = input("Which inv?")
                if answer in inventories:
                    update(answer)
                    break
                if answer == "exit":
                    break
                continue
        if answer == 3:
            print("Goodbye")
            break
        else:
            continue


browse()
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172

1 Answers1

0
    inventory = {}
    items = {}

is creating shared attributes for all class instances. You should initialize the attributes in the __init__ method.

class inventory:
    def __init__(self, name):
        self.name = name
        self.inventory = {}
        self.items = {}
Barmar
  • 741,623
  • 53
  • 500
  • 612