<p>很抱歉对您的代码进行了大量的修改,但我认为这更像是您的目标:</p>
<pre><code>class EbayInventory:
def __init__(self):
self.all_inventory = []
def print_items(self):
print('Current item list by index:')
for i in range(0, len(self.all_inventory)):
print("{} -> {}".format(i+1, self.all_inventory[i]))
def add_item(self):
manufacturer = input("Enter Manufacturer: ")
object_type = input("Enter what the item is: ")
price = input("Enter price: ")
item = {'manufacturer': manufacturer, 'type': object_type, 'price': price}
self.all_inventory.append(item)
print("Item added successfully")
def delete_item(self):
self.print_items()
delete = int(input("Item id you want to delete: "))
try:
del self.all_inventory[delete - 1]
print("The item entered has been deleted.")
except Exception as e:
print("An error occurred deleting that item, details below")
print(e)
def sale_status(self):
self.print_items()
update_index = int(input("Item id you want to update: "))
if update_index > len(self.all_inventory) or update_index <= 0:
print("You're trying to change an item that doesn't exist!!")
return
print("OK. Let's get that item up to date!")
manufacturer = input("Enter Manufacturer: ")
object_type = input("Enter what the item is: ")
price = input("Enter price: ")
item = {'manufacturer': manufacturer, 'type': object_type, 'price': price}
self.all_inventory[update_index - 1] = item
print("OK. We got that update taken care of")
if __name__ == "__main__":
my_app = EbayInventory()
while True:
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("Please enter the number corresponding to how you would like help: ")
if user_wants == "1":
my_app.add_item()
elif user_wants == "2":
my_app.delete_item()
elif user_wants == "3":
my_app.sale_status()
elif user_wants == "4":
my_app.print_items()
elif user_wants == "5":
print("Thank you for using item inventory.")
break
else:
print("Input not understood. Please try again.")
</code></pre>
<p>你有一个变量<code>user</code>什么都没做。只需使用<code>while True:</code>进入无限循环即可。如果你想像以前那样循环,那么你可以用<code>user = False</code>来代替<code>break</code>。这有时是一个漂亮的把戏,但在这里没有意义,我想。在我看来,库存实际上是存储在类中唯一有好处的东西,然后方法可以通过<code>self.all_inventory</code>访问它来调整它。我将您的代码移到<code>add_item()</code>和<code>sale_status()</code>方法中请求项输入,因此现在主代码块看起来干净多了。我还将它包装在<code>if __name__ == "__main__":</code>中,这样您就可以将这个类导入到另一个项目中,而不必运行整个程序!我用<code>try:</code>和<code>except:</code>子句进行了一些基本的错误检查。我想你误解了<code>Class</code>和<code>Instance</code>之间的区别。所以在我的代码中,<code>Class</code>是EbayInventory,而<code>Instance</code>是<code>my_app</code>。你创建类的实例就像我对<code>my_app = EbayInventory()</code>所做的那样,然后self现在引用我的应用程序。这样我就可以调用<code>my_app.add_item()</code>。不过,您可以有多个对象实例,它们在您的计算机内存中都有自己的空间。所以你可以说:</p>
<pre><code>app1 = EbayInventory()
app2 = EbayInventory()
app1.add_item()
</code></pre>
<p>只有app1会有任何项,而app2仍然是一个空列表,但是仍然有<code>methods</code>来通过<code>app2.add_item()</code>构建一个清单。不过,为了回答您的主要问题,您从不调用函数<code>Inventory()</code>,因此它不存在来保存您的信息。当您调用<code>item_info = Ebay_Inventory(manufacturer, object_type, price)</code>时,实际上您正在创建一个类的<strong>实例</strong>,在这里这样做是非常荒谬的,因为这意味着在下一行您可以说<code>item_info.add_item()</code>,这将使<strong>成为另一个<strong>实例,但是您没有将此信息保存在任何地方,因此您永远无法检索它</p>