1 # 所有消费记录
 2 customer_bill = []
 3 
 4 # 零售
 5 def sale():
 6     print("=" * 38)
 7     print("1. 乌龙茶  20".center(30))
 8     print("2. 茉莉花茶  80".center(30))
 9     print("3. 绿茶  8".center(30))
10     print("4. 红茶  4.5".center(30))
11     print("=" * 38)
12 
13     this_bill = {}
14     money = 0  # 用来记录总价格
15     while True:
16         goods = input("请输入您要购买的商品序号:")
17         if not goods:  # "" --> False
18             print("总共需要支付: %.2f元" % money)
19             u_money = float(input("请输入您支付的金额:"))
20             print("本次支付%.2f元, 应找零%.2f元" % (u_money, u_money - money))
21             this_bill["消费金额"] = money
22             this_bill["支付金额"] = u_money
23             this_bill["找零金额"] = u_money - money
24             customer_bill.append(this_bill)  # 记录本次消费记录
25             break  # 如果啥也没输入,代表不买了,跳出循环
26         count = int(input("请输入您要购买的数量:"))
27 
28         if goods == "1":
29             this_bill["乌龙茶"] = count
30             money += 20 * count
31         elif goods == "2":
32             this_bill["茉莉花茶"] = count
33             money += 80 * count
34         elif goods == "3":
35             this_bill["绿茶"] = count
36             money += 8 * count
37         elif goods == "4":
38             this_bill["红茶"] = count
39             money += 4.5 * count
40         else:
41             print("您当前输入有误, 请重新输入")
42             continue
43 
44 
45 # 查询消费记录
46 def query_bill():
47     u_input = input("请输入您要哪次的消费记录,如不输入,代表查询全部:")
48     if u_input == "":  # 查询全部
49         for bill in customer_bill:  # 遍历获取每次的消费记录
50             print("-----------------------")
51             for k, v in bill.items():  # 遍历一次消费记录, 获取其中存储的键值对
52                 print(k, v)
53     else:
54         count = int(u_input) - 1  # 第一次: 1 ---> list 索引为0
55         bill = customer_bill[count]
56         print("~~~~~bill~~~~~~")
57         for k, v in bill.items():
58             print(k, v)
59 
60 
61 # 主函数
62 def main():
63     while True:
64         print("=" * 38)
65         print("欢迎来到嘻嘻牌自动贩卖机".center(30))
66         print("1.零售".center(30))
67         print("2.退出".center(30))
68         print("3.查询消费记录".center(30))
69         print("=" * 38)
70 
71         res = input("请输入您的选择:")
72 
73         if res == "1":
74             sale()
75         elif res == "3":
76             query_bill()
77         elif res == "2":
78             break
79         else:
80             print("您输入有误, 请仔细阅读,重新输入")
81 
82 
83 if __name__ == "__main__":
84     main()