【发布时间】:2016-11-19 09:05:15
【问题描述】:
我的 Order 类中有一个字典 self.total = defaultdict(int),用于输入和打印在我的订购系统中订购的每件商品的总数。对象存储为
coffee_available=[Coffee(1, "1: Flat White", 3.50),
Coffee(2, "2: Long Black", 3.50),
Coffee(3, "3: Cappuccino", 4.00),
Coffee(4, "4: Espresso", 3.25),
Coffee(5, "5: Latte", 3.50)]
`
我“订购”这些对象的方式是在我的订单类中调用一个函数
def order(self, order_amount, coffee_id):
self.total[coffee_id] += order_amount
我使用订单类中的另一个函数打印每个项目类型及其各自的订单金额
def print_order(self):
print(" ")
print("Coffee Type\t\tAmount of Times ordered")
print("-----------\t\t-----------------------")
for coffee in coffee_available:
print("{}\t- - -\t {} ".format(coffee.coffee_type, self.total[coffee.coffee_id]))
这很好用,因为每次我的代码在同一个会话中运行时,它每次都会存储order_amount 值并正确显示它,唯一的问题是,如果我终止程序,它不会存储何时的数据我接下来打开它。如果我要永久存储数据,我会怎么做?我应该永久存储什么?
【问题讨论】:
-
python
pickle库是关键:wiki.python.org/moin/UsingPickle
标签: python python-3.x