1 # ----user.txt---- 2 3 {'已购商品': '', '消费记录': '', '余额': 0} 4 5 6 # ----commodity.txt---- 7 8 iPhone,5000 9 HuaWei,300010 XiaoMi,200011 oppo,1000
1 #/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 # Day2/file_shopping.py 4 5 _author_ = 'hepidong' 6 7 import time 8 9 # ---------------------------------------- # 10 # 程序:购物车-用户入口 11 # 需求: 12 # 商品信息保存在文件中 13 # 已购商品,余额记录 14 15 16 def user(): 17 "购物车-用户" 18 for key in uu: 19 print ('您的%s:%s' % (key, uu[key])) 20 if key == '余额': 21 balance = uu[key] 22 elif key == '已购商品': 23 if uu[key] == '': 24 ShoppingCart = [] 25 continue 26 else: 27 ShoppingCart = [uu[key]] 28 elif key == '消费记录': 29 if uu[key] == '': 30 record = [] 31 continue 32 else: 33 record = [uu[key]] 34 35 salay = int(input('请输入您的工资:')) 36 balance += salay 37 uu['余额'] = balance 38 uw = open('user.txt', 'w', encoding='utf-8') 39 uw.write(str(uu)) 40 uw.close() 41 42 while True: 43 comm = com.split('\n') 44 commm = [] 45 for i in comm: 46 if i == '': 47 break 48 commm.append(i.split(',')) 49 50 for i in commm: 51 num = commm.index(i) 52 print (num + 1,'-', i) 53 54 your_num = input("请选择商品编号(q退出):") 55 if your_num.isdigit(): # 判断是否为数字 56 your_num = int(your_num) 57 if your_num <= len(commm): 58 if int(commm[your_num - 1][1]) <= balance: 59 balance = balance - int(commm[your_num - 1][1]) 60 ShoppingCart.append(commm[your_num -1]) 61 print ('购买成功!') 62 elif int(commm[your_num - 1][1]) > balance: 63 print ('您的余额已不足购买本商品!请重新选择或退出(q)') 64 else: 65 print ('输入错误,请重新输入!') 66 continue 67 elif your_num == 'q': 68 print ('您已购买了', ShoppingCart) 69 print ('购物结束,您的余额为%d元。' % balance) 70 uu['余额'] = balance 71 shop = [] 72 record_new = [] 73 for i in range(len(ShoppingCart)): 74 if len(ShoppingCart[i]) == 2: 75 shop.append(ShoppingCart[i][0]) 76 record_new.append((time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), ShoppingCart[i])) 77 else: 78 shop.append(ShoppingCart[i]) 79 uu['已购商品'] = shop 80 uu['消费记录'] = record_new 81 uw = open('user.txt', 'w', encoding='utf-8') 82 uw.write(str(uu)) 83 uw.close() 84 exit() 85 else: 86 print ('输入错误,请重新输入!') 87 continue 88 89 uu['余额'] = balance 90 uu[key] = ShoppingCart 91 uw = open('user.txt', 'w', encoding='utf-8') 92 uw.write(str(uu)) 93 uw.close() 94 95 96 97 # ---------------------------------------- # 98 # 程序:购物-商家入口 99 # 需求:100 # 可以添加商品,修改商品价格101 102 def commodity():103 "商家入口"104 while True:105 print ("add. 增加商品\nupdata. 修改商品价格\nq. 退出")106 ss = input('请选择操作:')107 if ss == 'add':108 comm_name = input("商品名称:")109 comm_sum = input('商品金额:')110 c.write('\n' + comm_name + ', ' + comm_sum)111 c.flush()112 elif ss == 'updata':113 up_name = input("修改的商品名称:")114 up_sum = input("修改金额为:")115 up_data = ''116 c.seek(0)117 for line in c:118 if up_name in line:119 line = line.replace(line, (up_name + ',' + up_sum + '\n'))120 up_data += line121 with open('commodity.txt', 'w+', encoding='utf-8') as cs:122 cs.write(up_data)123 cs.flush()124 elif ss == 'q':125 exit()126 else:127 print ('输入错误,请重新输入!')128 continue129 130 131 132 133 # ---------------------------------------- #134 # 程序:启动135 136 u = open('user.txt', 'r+', encoding='utf-8') # 用户信息137 c = open('commodity.txt', 'r+', encoding='utf-8') # 商品信息138 # with open('user.txt', 'r+', encoding = 'utf-8') as u:139 # with open('commodity.txt', 'r+',encoding = 'utf-8') as c:140 141 com = c.read()142 uu = eval(u.read())143 144 while True:145 print ('1. 用户\n2. 商家')146 x = input('请选择角色(q退出):')147 if x == '1':148 user()149 elif x == '2':150 commodity()151 elif x == 'q':152 exit()153 else:154 print ('输入错误,请重新输入!')155 156 157 u.close()158 c.close()