购物车

 1 product_list = [
 2     ('Iphone',5800),
 3     ('Mac Pro',9800),
 4     ('Bike',800),
 5     ('Watch',10600),
 6     ('Coffee',31),
 7     ('Alex Python',120)
 8 ]
 9 shopping_list = []
10 salary = input('输入你的工资:')
11 if salary.isdigit():     #检测字符串是否为数字
12     salary = int(salary )
13     while True:
14         for index,item in enumerate(product_list):
15             print(index +1,item)
16         user_choice = input("选择买什么?》》:")
17         if user_choice.isdigit():
18             user_choice = int(user_choice )
19             if user_choice < len(product_list) and user_choice >= 0:#检测商品是否在列表中
20                 p_item = product_list[user_choice-1]
21 
22                 if p_item[1] <= salary: #检测工资是否足够
23 
24                     shopping_list.append(p_item)
25                     salary -= p_item[1]
26                     print("Added %s into shopping cart,your current balance is \033[41;1m%s\033[0m" %(p_item,salary ))
27                 else:
28                     print("\033[31;1m你买不起\033[0m")
29 
30             else:
31                 print("product code [%s] is not exist!" % user_choice)
32         elif user_choice == "q":
33             print("-------------shopping list-----------")
34             for p in shopping_list:
35                 print(p)
36             print("你的余额:",salary )
37             exit()
38         else:
39             print('invalid option')
简单购物

 

 1 #商家入口
 2 #增加商品
 3 def add(commo):
 4     arg = eval(commo)
 5     commodity_list= 'commodity {}'.format(arg['commodity'])  # 要插入backend的字段
 6     price_list = "price {}".format(arg['price'])
 7     add_flag = True
 8     with open('shopping_list', 'r+', encoding='utf-8') as f:
 9         for line in f:
10             if line.strip() == commodity_list:
11                 print("您输入的商品已经存在")
12                 add_flag = False
13             if add_flag:
14                 f.write('\n{}'.format(commodity_list))
15                 f.write('\n\t\t  {}'.format(price_list) )
16  #修改商品价格
17 def update(commo):
18     arg = eval(commo)
19     #print(arg)
20     commodity_list= 'commodity {}'.format(arg['commodity'])  # 要插入backend的字段
21     price_list = "price {}".format(arg['price'])
22     #print(commodity_list )
23     update_flag = False
24     update_f = False
25     with open('shopping_list', 'r', encoding='utf-8') as f,\
26         open('shopping_list_1','w') as f1:
27         for line in f:
28             #print(line)
29             if line.strip() == commodity_list:
30                 update_flag = True
31                 continue
32             if line.strip().startswith('commodity') and line.strip() != commodity_list:
33                 update_flag = False
34             if not update_flag:
35                 f1.write(line)
36             if update_flag and not update_f:
37                 f1.write("{}".format(commodity_list ) )
38                 f1.write('\n\t\t  {}\n'.format(price_list ) )
39                 update_f = True
40     with open('shopping_list', 'w', encoding='utf-8') as f,\
41        open('shopping_list_1','r') as f1:
42         for line in f1:
43             f.write(line )
44 #后台管理
45 while True:
46     print('欢迎进入购物车后台操作:'.center(50, '-'))
47     print('\n 1添加商品\n')
48     print('2 修改商品价格\n')
49 
50     op_shopping = input("请输入需要进入的模式")
51     if op_shopping == '1':
52         commo = input("请输入要新增的商品:"
53                       "例如:{'commodity': 'Iphone','price':'20'}\n")
54         add(commo)
55     elif op_shopping == '2':
56         commo = input("请输入要修改价格的商品"
57                       "例如:{'commodity': 'Iphone','price':'20'}\n")
58         update(commo)
59     elif op_shopping =='q':
60         pass
61     else:
62         print('请正确输入模式')
商家入口

 

posted @ 2019-07-01 19:00  普罗米修斯-  阅读(105)  评论(0编辑  收藏  举报