-1

I am new to programming and have tried to create a product and inventory class and I'm trying to add a new entry into the inventory through the 'add_product' function in the Inventory class. I keep getting a "<main.Product at 0x7fdbcc6c4460>]" error , not sure what I'm doing wrong ? appreciate the help:

product_database = [
    {'product_id': 1,'product_name': 'Computer','price': 299.99,'qty': 10},
    {'product_id': 2,'product_name': 'Speakers','price': 699.99,'qty': 20},
    {'product_id': 3,'product_name': 'Monitor','price': 415.00,'qty': 15},
    {'product_id': 4,'product_name': 'Keyboard','price': 899.99,'qty': 8},
    {'product_id': 5,'product_name': 'Guitar','price': 1051.00,'qty': 25},
    {'product_id': 6,'product_name': 'Banjo','price': 350.00,'qty': 5},
]
​
​
class Product():
 
    def __init__(self,product_id,product_name,price,qty):
        self.product_id = product_id
        self.product_name = product_name
        self.price = price
        self.qty = qty
        
    def __str__(self):
        return self.products
​
class Inventory():
    
    def __init__(self):
        self.products = []
        for item in product_database:   
            self.products.append(item)
      
​
    def __str__(self):
        return str(self.products)
​
    
    def add_product(self):
        idnum = int(input('Enterid: '))
        name = input('Enter Product Name: ')
        price = int(input('Enter Price: '))
        quantity = int(input('Enter quantity: '))
        self.products.append(Product(idnum, name, price, quantity))
        return self.products

Result

[2]:

products = Inventory()
print(products)
[{'product_id': 1, 'product_name': 'Computer', 'price': 299.99, 'qty': 10}, {'product_id': 2, 'product_name': 'Speakers', 'price': 699.99, 'qty': 20}, {'product_id': 3, 'product_name': 'Monitor', 'price': 415.0, 'qty': 15}, {'product_id': 4, 'product_name': 'Keyboard', 'price': 899.99, 'qty': 8}, {'product_id': 5, 'product_name': 'Guitar', 'price': 1051.0, 'qty': 25}, {'product_id': 6, 'product_name': 'Banjo', 'price': 350.0, 'qty': 5}]
[3]:

products.add_product()
Enterid:  7
Enter Product Name:  interface
Enter Price:  450
Enter quantity:  10
[3]:
[{'product_id': 1, 'product_name': 'Computer', 'price': 299.99, 'qty': 10},
 {'product_id': 2, 'product_name': 'Speakers', 'price': 699.99, 'qty': 20},
 {'product_id': 3, 'product_name': 'Monitor', 'price': 415.0, 'qty': 15},
 {'product_id': 4, 'product_name': 'Keyboard', 'price': 899.99, 'qty': 8},
 {'product_id': 5, 'product_name': 'Guitar', 'price': 1051.0, 'qty': 25},
 {'product_id': 6, 'product_name': 'Banjo', 'price': 350.0, 'qty': 5},
 <__main__.Product at 0x7fdbcc6c4460>]
azro
  • 53,056
  • 7
  • 34
  • 70
mac95782
  • 71
  • 7

1 Answers1

1

That is not an error, <__main__.Product at 0x7fdbcc6c4460> means a Product instance, the other elements are dict, this one is a Product instance, to show it properly you need to override __repr__ like

def __repr__(self):
    return f"{self.product_id}:{self.product_name} costs {self.price} qty:{self.qty}"

But if you need coherence, you may have only Product instances or only dict

  • only Product

    def __init__(self):
        self.products = []
        for item in product_database:
            self.products.append(Product(*item.values()))
    
  • only dict

    def add_product(self):
        idnum    = int(input('Enterid: '))
        name     = input('Enter Product Name: ')
        price    = int(input('Enter Price: '))
        quantity = int(input('Enter quantity: '))
        self.products.append({'product_id': idnum, 'product_name': name, 'price': price, 'qty': quantity})
    

You can read more at Difference between __str__ and __repr__?

azro
  • 53,056
  • 7
  • 34
  • 70