Python之购物车

Python之购物车

 1 msg_list = [
 2         ['iphone',8888],
 3         ['coffe',38],
 4         ['book',90],
 5         ['Tesla',100000],
 6         ['RR',1000000000]
 7     ]
 8 
 9 
10 shopping_cart = []
11 
12 salary = int(input('input your salary:'))
13 
14 while True:
15     #计数器,给列表的物品赋值
16     index = 0
17     # 循环列表msg_list,并打印
18     for product in msg_list:
19         print(index,product)
20         index +=1
21     #输入要选取的物品编号
22     choice = input('>>:').strip()
23     #对输入的类型进程判断,看是否是数字,isdigit是判断是否是数字的方法
24     if choice.isdigit():
25         #判断输入的数字是否在列表的索引值范围内
26         choice = int(choice)
27         if choice >= 0 and choice <= len(msg_list):
28             #商品存在,取到商品。
29             product = msg_list[choice]
30             #判断输入的金额是否买得起。
31             if product[1] <= salary:
32                 #买得起,并加入购物车。
33                 shopping_cart.append(product)
34                 #总金额减去加入购物车的钱
35                 salary -= product[1]
36                 # \033[31;1m XXXXXXXX \033[0m  固定写法,可以改变XXXXXX的字体颜色
37                 print('Added product:' + product[0] + ' into shopping cart, \033[31;1myour current\033[0m balance:' + str(salary))
38             else:
39                 print('买不起了,产品价格是' + str(product[1]) + "还差" + str(product[1]-salary) + '')
40         else:
41             print('商品不存在')
42     elif choice == 'q':
43         print('-------已购买商品-------')
44 
45         for i in shopping_cart:
46             print(i)
47 
48         print('您的余额为:',salary)
49         print('---------结束----------')
50         break
51     else:
52         print('无此选项')

 

购物车优化:

 1 msg_list = [['IphoneX',8888],
 2             ['Coffee',30],
 3             ['MINI',300000],
 4             ['Python',998],
 5             ['LAND ROVER',4000000],
 6             ['RR',10000000],
 7             ]
 8 
 9 shopping_cart = {}
10 
11 salary = int(input("input your salary:"))
12 
13 while True:
14     index = 0
15     for product in msg_list:
16         print(index,product)
17         index +=1
18     choice = input(">>:").strip()
19     if choice.isdigit(): #判断是否为数字
20         choice = int(choice)
21         if choice >= 0 and choice < len(msg_list):#商品存在
22             product = msg_list[choice] #取到商品
23             if product[1] <= salary:   #判断能否买的起
24                 #买的起
25                 if product[0] in shopping_cart: #之前购买过呀
26                     #字典的取值方法
27                     shopping_cart[product[0]][1]  += 1 #[price, 数量],只需要把数量+1,加入购物车
28                 else:
29                     shopping_cart[product[0]] = [product[1], 1]  #创建一条商品购买记录
30 
31                 salary -= product[1] #扣钱
32                 print("Added product " + product[0] + " into shopping cart,\033[42;1myour current\033[0m balance " + str(salary) )
33             else:
34                 print("买不起,穷逼! 产品价格是" + str(product[1])  + " 你还差" + str(product[1]-salary) + "")
35         else:
36             print("商品不存在!")
37     elif choice == "q":
38         print("-------已购买商品列表--------")
39         #print(shopping_cart)
40         id_counter = 1
41         #初始化一个总花费的变量
42         total_cost = 0
43         print("id    商品    数量    单价    总价")
44         for key in shopping_cart:
45             #字符串格式化,10是每个占位符的距离  
46             print("%10s%10s%10s%10s%10s" %(id_counter,
47                   key,
48                   shopping_cart[key][1],
49                   shopping_cart[key][0],
50                   shopping_cart[key][1]*shopping_cart[key][0]))
51 
52             id_counter +=1
53             total_cost += shopping_cart[key][1]*shopping_cart[key][0] #单个商品总价
54 
55         print("您的总花费为:",total_cost)
56         print("您的余额为:",salary)
57         print("----------end----------")
58         break
59     else:
60         print("无此选项!")

 

posted @ 2018-04-28 16:45  王先生是胖子  阅读(148)  评论(0编辑  收藏  举报