Python根据用户输入保持总计数

时间:2016-07-12 10:51:17

标签: python python-3.x

我的商品订购系统中有一个功能,其目标是保存订购商品的总数并打印每件商品及订购金额

class Totals(object):

def __init__(self, total):
    self.total = total

def total(order_amount, coffee_id):
    count = 0
    print("Coffee Type\t\tAmount of Times ordered")                       
    print("-----------\t\t-----------------------")
    for coffee in coffee_available:

        if coffee.coffee_id == coffee_id:
            count = order_amount
            coffee_id = order_coffee

            print("{}\t- - -\t {} ".format(coffee.coffee_type, count))

有了这个,我只能打印一个项目,它确实显示了该项目的订购数量,但同样只会对一个项目执行此操作

该功能基于用户输入,项目为

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)]

如何更改功能以便打印所有项目并跟踪每次调用时所订购的项目数量,以便在我的代码循环多次后仍显示每个项目及其次数被命令

好了,我现在有一种方法可以打印每种类型的咖啡,但会为所有商品打印1份商品的咖啡量,并且不会保留每件商品所需的咖啡量

 class Order(object):

def __init__(self):
    self.total = {}

def order(self, order_amount, coffee_id):
    if coffee_id not in self.total.keys():
        self.total[coffee_id] = 0
    self.total[coffee_id] += order_amount


def print_order(self, coffee_id):
    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_id]))

这就是我称呼它的方式

        new_order = Order()
        new_order.order(order_amount, coffee_available[order_coffee - 1])
        new_order.print_order(coffee_available[order_coffee - 1])

任何建议都会很棒

2 个答案:

答案 0 :(得分:2)

您应该保存一张字典,该字典会从咖啡ID到订单金额进行映射,并在每个订单上进行更新。

class Totals(object):

    def __init__(self):
        self.total = {}

    def order(self, order_amount, coffee_id):
        if coffee_id not in self.total.keys():
            self.total[coffee_id] = 0
        self.total[coffee_id] += order_amount

对于打印,您应该添加一个打印功能,可以根据需要打印self.total

答案 1 :(得分:0)

我个人认为您应该管理所订购的物品。此示例具有将Coffee对象映射到其订购次数的字典。

class Order(object):
    ID = 0
    def __init__(self):
        self._orders = {}
        self._order_id = Order.ID
        Order.ID += 1

    def add_item(self, coffee):
        # Gets the current quantity for the coffee
        # If no previous order qty defaults to 1
        qty = self._orders.get(coffee, 1)
        # Add / update dict with coffee object and qty
        self._orders[coffee] = qty

    def display_order(self):
        print("Coffee Type\t\tAmount of Times ordered")                       
        print("-----------\t\t-----------------------")
        # For each coffee object ordered | sorted by coffee id
        for coffee in sorted(self._orders, key = lambda item: item.coffee_id):
            print("{:<10}\t- - -\t {} ".format(coffee.coffee_type,
                                               coffee.price * self._orders[coffee]))