按价格降序

时间:2013-11-26 03:54:29

标签: python

NEW EDIT

当我只输入1杯咖啡时,结果是这样的

Item: Juice Cost:  OrderCount: 1                                                                                                                                                                                                     
Item: Coffee Cost:  OrderCount: 1                                                                                                                                                                                                    
Item: Lemonade Cost:  OrderCount: 1                                                                                                                                                                                                  
Item: Soda Cost:  OrderCount: 1                                                                                                                                                                                                      
Item: Water Cost:  OrderCount: 1                                                                                                                                                                                                     
7% TAX:                                                                                                                                                                                                                              
                         -------                                                                                                                                                                                                          
TOTAL BALANCE:                                                                                                                                                                                                                       
-------------------------------- 

这是相关的代码段:

if totaldrinks > 0:
    print("DRINK")
    d = {}
    if coffee > 0:
        d['Coffee'] = (coffee, drink.count(countcoffee)) # d[menu_item] = (cost, order_count)
    if juice > 0:
        d['Juice'] = (juice, drink.count(countjuice)) # d[menu_item] = (cost, order_count)
    if soda > 0:
        d['Soda'] = (soda, drink.count(countsoda)) # d[menu_item] = (cost, order_count)
    if water > 0:
        d['Water'] = (water, drink.count(countwater)) # d[menu_item] = (cost, order_count)
    if lemonade > 0:
        d['Lemonade'] = (lemonade, drink.count(countlemonade)) # d[menu_item] = (cost, order_count)
    d_lst = sorted(d.items(), key=lambda x:x[1], reverse = True)
    for item in d_lst:
        print("Item: %s Cost: $%.2f OrderCount: %i "%(item[0], item[1][0], item[1][1]))

2 个答案:

答案 0 :(得分:1)

您的代码没有正确缩进。如果我理解你的问题,下面的代码应该为你完成这个工作:

创建一个词典:

d = {}

d['coffeec'] = (2.00, 2) # d[menu_item] = (cost, order_count)
d['juciec'] = (2.00, 1) # d[menu_item] = (cost, order_count)
d['sodac'] = (1.50, 10) # d[menu_item] = (cost, order_count)
d['waterc'] = (1.00, 20) # d[menu_item] = (cost, order_count)
d['lemonadec'] = (1.50, 10) # d[menu_item] = (cost, order_count)

所以,

d = {'coffeec': (2.0, 2), 'waterc': (1.0, 20), 'lemonadec': (1.5, 10), 'juciec': (2.0, 1), 'sodac': (1.5, 10)}

然后根据价格反向排序:

d_lst = sorted(d.items(), key=lambda x:x[1][0], reverse = True)

所以,新的d_lst如下:

d_lst = [('coffeec', (2.0, 2)), ('juciec', (2.0, 1)), ('lemonadec', (1.5, 10)), ('sodac', (1.5, 10)), ('waterc', (1.0, 20))]

然后遍历新的d_lst以打印收据:

>>> for item in d_lst:
...     print("Item:%s;;; Cost:%.2f;;; OrderCount:%i "%(item[0], item[1][0], item[1][1]))
...
Item:coffeec;;; Cost:2.00;;; OrderCount:2
Item:juciec;;; Cost:2.00;;; OrderCount:1
Item:lemonadec;;; Cost:1.50;;; OrderCount:10
Item:sodac;;; Cost:1.50;;; OrderCount:10
Item:waterc;;; Cost:1.00;;; OrderCount:20

答案 1 :(得分:0)

根据您的具体问题提供更完整的代码,按原样运行并回复是否达到您的具体任务:

#!/usr/bin/python

def main():
    #welcome statement
    print("Welcome to Deluxe Cafe")
    print("Please select by entering the selections from the menu below.")
    print("-------------------------------------------------------------------------------")
    #drinks_dict: key = number;;; value = (name of the drink, cost, order_count)
    drinks_dict = {'1': ['Coffee', 2.00,0], '2': ['Juice', 2.00, 0], '3': ['Soda', 1.50, 0], '4': ['Water', 1.00, 0], '5': ['Lemonade', 1.50, 0]}
    #ask for customer input
    while True:
        for k,v in sorted(drinks_dict.items()):
            print("%s. %s  [%.2f]"%(k, v[0], v[1]))
        order = raw_input("Please enter your selection: ")

        if order in drinks_dict:
            print("1x %s added "%drinks_dict[order][0])
            drinks_dict[order][2] += 1
            more_orders = raw_input("Do you want to order more? yes or no\n")
            if more_orders == 'no':
                break
        else:
            print('Please type either: 1 or 2 or 3 or 4 or 5')
    drinks_list = []
    drinks_list = sorted(drinks_dict.items(), key = lambda x: x[1][1], reverse = True)
    if drinks_list:
        print("Cafe Receipt")
        for item in drinks_list:
            if item[1][2] > 0:
                print("Item:%s;;; Cost:%.2f;;; OrderCount:%i "%(item[1][0], item[1][1], item[1][2]))

if __name__ == '__main__':
    main()