〖Demo〗-- 购物车

【购物车】

 

1. 商品信息- 数量、单价、名称
2. 用户信息- 帐号、密码、余额
3. 用户可充值
4. 购物历史信息
5. 允许用户多次购买,每次可购买多件
6. 余额不足时进行提醒
7. 用户退出时 ,输出档次购物信息
8. 用户下次登陆时可查看购物历史
9. 商品列表分级显示

  1 # 用户登录
  2 T = True
  3 name = input('Please enter name:')
  4 f = open('shopping.txt','r')
  5 gg = f.read()
  6 f.close()
  7 ff = gg.split()
  8 count = 0
  9 for cc in ff: # 检查是否新用户
 10     bb = cc.split(',') # 分成不同列表
 11     if bb[0] == name:
 12         qq = bb[2]
 13         while T:
 14             password = input('Please enter password:')
 15             if password == qq:
 16                 balance = bb[1]
 17                 print ('Welome! Your balance is %s' % balance)
 18                 count=+1
 19                 break
 20             else:
 21                 print('Your password was wrong.')
 22         break
 23     else:
 24         continue
 25 if count == 0:
 26     password = input('Please enter password:')
 27     print('Welcome to the first! Your balance is 0')
 28     balance = 0
 29 
 30 # 创造三级菜单
 31 shoppingmall = {'sport': {'clothes':{'coat':{20: 80}, 'shirt':  {30: 100}, 'skirt': {20: 120}},
 32                             'tools': {'net': {20: 70}, 'bat': {40: 100}, 'fish pole': {20: 90}},
 33                             'ball': {'basketball': {90: 200}, 'pingpong': {10: 20}, 'football': {50: 170}}},
 34                 'electrical equipment': {'phone': {'Apple': {10: 6000}, 'Xiaomi': {15: 2000}, 'Huawei': {5: 3000}},
 35                           'computer': {'HP': {2: 4000}, 'Lenovo': {6: 2200}, 'Dell': {10: 3000}},
 36                           'TV': {'Sony': {60: 5000}, 'LG': {30: 3000}, 'TCL': {40: 2200}}},
 37                 'book': {'baby': {'Story': {50: 37},'Picture': {30: 64},'Music': {60: 93}},
 38                           'child': {'Exercise': {30: 36},'Earth': {20: 55},'Weather': {30: 64}},
 39                           'adult': {'Leaning': {40: 45},'Cookbook': {50: 74},'Tourism': {50: 43}}}}
 40 list1=[]
 41 list2=[]
 42 list3=[]
 43 shopping_list=[]
 44 item_price={}
 45 lse = []
 46 
 47 # 打印一级菜单
 48 while T:
 49 
 50     while T:
 51         for key in shoppingmall:
 52             print(key)
 53             list1.append(key)
 54 # 选择一项到下一级菜单或者退出
 55         choose = input('Please choose thing or q = quit:')
 56         if choose == 'q':
 57             exit('Bye!')
 58 # 进入下一级菜单
 59         elif choose in list1:
 60             a = shoppingmall.get(choose)
 61             while T:
 62                 for key1 in a:
 63                     print(key1)
 64                     list2.append(key1)
 65             # 选择一项到下一级菜单或者退出或返回
 66 
 67                 choose1 = input('Please chose that you want or q = quit or b = back:')
 68                 if choose1 == 'q':
 69                     exit('Bye!')
 70                 elif choose1 == 'b':
 71                     break
 72                 elif choose1 in list2:
 73                 # 进入下一级菜单
 74                     b = a.get(choose1)
 75                     while T:
 76                         for key2 in b:
 77                             c = b.get(key2)
 78                             list3.append(key2)
 79                             for number in c:
 80                                 price = c.get(number) #number剩余数量,price 商品价钱
 81                                 item_price[key2] = price#商品和对应的价钱放入字典中
 82                                 print ('%s is $%s left %s' % (key2, item_price[key2], number))
 83 # 选择退出,返回上一级菜单 或者第一个菜单
 84                         choose2 = input('What do you want to buy? n = quit,b = back.')
 85                         if choose2 == 'n':
 86                             exit('Bye!')
 87                         elif choose2 == 'b':
 88                             break
 89                         elif choose2 in list3:
 90                             quantity = int(input('Please choose quantity'))
 91                             if quantity <=number: #检查购买数量是否大于存货量
 92                                 if choose2 in shopping_list:
 93                                     local = shopping_list.index(choose2)
 94                                     quantity = int(quantity) + int(shopping_list[local+2])
 95                                     goods_price = (quantity * item_price[choose2])
 96                                     shopping_list.pop(local+1)
 97                                     shopping_list.pop(local+1)
 98                                     shopping_list.insert(local+1, goods_price)
 99                                     shopping_list.insert(local+2, quantity) #物品数量放入购物名单中
100                                 else:
101                                     shopping_list.append(choose2)
102                                     goods_price = (quantity * item_price[choose2])
103                                     shopping_list.append(goods_price)
104                                     shopping_list.append(quantity) # 物品数量放入购物名单中
105                                 shop_thing = shopping_list[::3]
106                                 shop_num = shopping_list[2::3]
107                                 print('Shopping list has:')
108                                 ii = len(shop_thing)
109                                 for item in range(ii): # 输出购买名单
110                                     print ('%s has %s'% (shop_thing[item], shop_num[item]))
111                                 last = input('Goods have been put in shopping_list, what do you want to do? '
112                                     'c = check out, q = quit, o = other thing I want to buy.')
113                                 if last == 'c':
114                                     #计算总数
115                                     total_price = 0
116                                     for h in range(len(shop_thing)):
117                                         total_price += goods_price
118                                     print('Total price is %s, your balance is %s' % (total_price, balance))
119                                     while T:
120                                         if total_price> int(balance): # 检查余额是否足够
121                                             out = input('Balance is not enough,do you want to rechange? y =yes,'
122                                                         'q = quit')
123                                             if out == 'y':
124                                                 recharge = input('How much do you recharge') # 余额不足,需要充值
125                                                 if int(recharge) and int(recharge)>0:
126                                                     balance = int(balance) + int(recharge)
127                                                 else:
128                                                     one_more = input('Invalid value, any key = please again, q = quit.')
129                                                     if one_more == 'q':
130                                                         exit()
131                                             elif out == 'q':
132                                                 exit()
133                                         else:
134                                             balance = int(balance) - total_price # 计算总价,余额,输出购买清单
135                                             shopping_list[-1] = balance
136                                             print ('This you shopping list'.center(40,'*'))
137                                             aa = len(shop_thing)
138                                             for item1 in range(aa):
139                                                 print ('%s: %s'% (shop_thing[item1], shop_num[item1]))
140                                             print ('Total price is %s, balance is %s' % (total_price, balance))
141                                             end = 'Thank you, please come again'.center(40, '*')
142                                             print (end)
143                                             if count == 0: # 购买的历史信息写入文件
144                                                 f = open('shopping.txt', 'a') #这次购买物品放入文本中
145                                                 msg = '\n%s,%s,%s\n' % (name, balance, password)
146                                                 f.write(msg)
147                                                 f.close()
148                                                 exit()
149                                             else:
150                                                 for cc in ff:
151                                                     bb = cc.split(',') # 分成不同列表
152                                                     dd = type(bb)
153                                                     if bb[0] == name:
154                                                         bb[1] = str(balance)
155                                                     ee = ','.join(bb).strip()
156                                                     lse.append(ee)
157                                                     ss = '\n'.join(lse)
158 
159                                                 f = open('shopping.txt', 'w') #这次购买物品放入文本中
160                                                 f.write(ss)
161                                                 f.close()
162                                             exit()
163                                 elif last == 'q':
164                                     exit()
165                                 elif last == 'o':
166                                     continue
167                                 else:
168                                     print ('Yout choose is over the choose, please choose again')
169                             else:
170                                 print ('Quantity is over, please choose again')
171                         else:
172                             print ('Yout choose is over the choose, please choose again')
173                 else:
174                     print('Sorry, your choose not in list! Please choose again')
175         else:
176              print('Sorry, your choose not in list! Please choose again')

 

posted @ 2017-08-27 22:54  盛欣  阅读(283)  评论(0编辑  收藏  举报