【发布时间】:2019-02-26 21:27:02
【问题描述】:
基本上,我正在制作一个应用程序来更好地帮助我管理我的 ebay 商店。我对编程和 OOP 还是很陌生。在看了一些教程之后,我拼凑了以下代码。到目前为止,一切都运作良好。我目前坚持的是,当用户输入库存物品时,它并没有保存它。而且,当用户想要查看库存时,他们添加的项目不会填充。任何输入或建议都会非常受用。
def Inventory():
All_Inventory = {}
class Ebay_Inventory:
def __init__(self, manufacturer, object_type, price):
self.manufacturer = manufacturer
self.object_type = object_type
self.price = price
def add_item(self):
manufacturer = input("Enter Manufacturer: ")
object_type = input("Enter what the item is: ")
price = input("Enter price: ")
item_info = Ebay_Inventory(manufacturer, object_type, price)
All_Inventory = item_info
print("Item added successfully")
def delete_item(self):
delete = input("What is the item you want to delete?: ")
if delete in All_Inventory.keys():
del[delete]
print("The item entered has been deleted.")
else:
print("Item not found")
def sale_status(self):
update = input("What is the item you want to update?:")
if update in All_Inventory.keys():
pass
else:
print("Item not found")
user=True
while user:
print("\n1. Add to item inventory")
print("2. Remove item from inventory")
print("3. Update sale status")
print("4. View inventory")
print("5. Exit program")
user_wants=input("What would you like to do today?")
if user_wants=="1":
Ebay_Inventory.add_item(input)
elif user_wants=="2":
Ebay_Inventory.delete_item(input)
elif user_wants=="3":
Ebay_Inventory.sale_status(input)
elif user_wants=="4":
print(All_Inventory)
elif user_wants=="5":
print("\n Thank you for using item inventory.")
break
elif user_wants!="":
print("\n Input not understood. Please try again.")
【问题讨论】:
-
您在类外的函数中定义
Inventory和all_inventory,但该函数的唯一目的是声明一个局部空集。然后,每次尝试添加项目时递归地创建一个新的 ebay_inventory 对象。这看起来像是函数式编程和面向对象编程的奇怪混搭