Python 在产品/库存类应用程序中获取0x7fdbcc6c4460处的]错误
我是编程新手,曾尝试创建产品和库存类,并尝试通过库存类中的“添加产品”功能向库存中添加新条目。我老是出错,不知道我做错了什么?感谢您的帮助:Python 在产品/库存类应用程序中获取0x7fdbcc6c4460处的]错误,python,python-3.x,Python,Python 3.x,我是编程新手,曾尝试创建产品和库存类,并尝试通过库存类中的“添加产品”功能向库存中添加新条目。我老是出错,不知道我做错了什么?感谢您的帮助: 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_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
结果
[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>]
这不是一个错误,意味着一个产品实例,其他元素是dict,这是一个产品实例,要正确地显示它,您需要覆盖
但如果您需要一致性,您可能只有产品实例或dict
唯一产品
只听写
你可以在
def __repr__(self):
return f"{self.product_id}:{self.product_name} costs {self.price} qty:{self.qty}"
def __init__(self):
self.products = []
for item in product_database:
self.products.append(Product(*item.values()))
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})