简单的购物车
【项目要求】
1、两个程序,商家的入口和客户的入口;
2、商家的入口要求:
(1)可以修改商品的价格,可以下架商品,上架商品,删除商品等操作。
3、客户的入口要求:
(1)用户登录时,在本地自己去查找该用户的salary;没有该用户信息则新建改用户信息,让其输入salary;
(2)用户购物完毕后打印用户消费的产品并打印salary;
(3)用户购物完毕后将用户的salary写进.txt中,在下次登陆时自动调用salary;
【流程图】
【代码】
~1、首先建两个.txt文本,存放商品信息和用户的信息:
commodity_of_merchant.txt info_of_client.txt
~2、商家入口代码:
1 # -*- coding:utf-8 -*- 2 #!/usr/bin/env python 3 # Author:suchagal 4 5 from __future__ import print_function 6 7 goods = {} 8 ss = [] 9 ss1 ={} 10 11 f = open(r'commodity_of_merchant.txt','rU') 12 s = f.readlines() 13 for i in range(0,len(s)): #将文本信息转化为字典形式的 14 ss = s[i].split() 15 ss1 = {ss[0]:ss[1]} 16 goods.update(ss1) 17 print('商品清单'.center(40,'*')) 18 for i1 in goods: 19 print(i1,goods[i1]) 20 21 while True: 22 choice = input('which do you choose? insert/delete/updata/select/quit:') #提示用户输入选择增/删/改/查 23 if choice == 'select': 24 for i_sel in goods: 25 print(i_sel,goods[i_sel]) 26 elif choice == 'insert': #增加商品 27 while True: 28 ins_good = input('input good which do you want to insert:') 29 if ins_good in goods.keys(): 30 print('the %s has be in the warehouse.'% ins_good) 31 continue 32 else: 33 ins_price = input('input the price of the %s:'% ins_good) 34 goods.update({ins_good:ins_price}) 35 break 36 elif choice == 'updata': #改商品价格 37 while True: 38 upd_good = input('input good which do you want to change price:') 39 if upd_good in goods.keys(): 40 upd_price = input('input the price of the %s :'%upd_good) 41 goods[upd_good] = upd_price 42 break 43 else: 44 print('the %s has not in the warehouse,again' % upd_good) 45 continue 46 elif choice == 'delete': #删除商品 47 while True: 48 del_good = input('input good which do you want to delete price:') 49 if del_good in goods.keys(): 50 goods.pop(del_good) 51 break 52 else: 53 print('the %s has not in the warehouse,again' % del_good) 54 continue 55 elif choice == 'quit': #推出程序 56 print('商品清单'.center(40,'*')) 57 for e in goods: 58 print(e,goods[e]) 59 f1 = open('commodity_of_merchant.txt', 'w') #将信息输出到.txt文本中 60 for mer in goods: 61 print('{0}\t{1}'.format(mer,goods[mer]),file=f1) 62 f1.close() 63 break 64 else: 65 print('input error!') 66 continue
~3、客户入口代码:
1 # -*- coding:utf-8 -*- 2 #!/usr/bin/env python 3 # Author:suchagal 4 5 goods = {} 6 ss1 = [] 7 sss1 ={} 8 9 users_name = {} 10 ss2 = [] 11 sss2 = {} 12 13 shopping_cart = [] 14 15 f1 = open(r'commodity_of_merchant.txt','rU') 16 s1 = f1.readlines() 17 for i in range(0,len(s1)): #格式化文本,字典类型输出 18 ss1 = s1[i].split() 19 sss1 = {ss1[0]:ss1[1]} 20 goods.update(sss1) 21 print('商品清单'.center(40,'*')) #输出commodity_of_merchant.txt的信息 22 for ii in goods: 23 print(ii,goods[ii]) 24 25 f2 = open(r'info_of_client.txt','rU') 26 s2 = f2.readlines() 27 for i1 in range(0,len(s2)): #格式化文本,字典类型输出 28 ss2 = s2[i1].split() 29 sss2 = {ss2[0]:ss2[1]} 30 users_name.update(sss2) 31 print('people list'.center(40,'*')) #输出在info_of_client.txt中的信息 32 for ii1 in users_name: 33 print(ii1,users_name[ii1]) 34 35 while True: 36 user_input = input('input your username:') 37 if user_input in users_name.keys(): #判断用户是否存在,若存在打印salary 38 print('you have \033[31;1m%s\033[0m left' % users_name[user_input]) 39 salary = int(users_name[user_input]) 40 else : 41 salary = int(input('input your salary:')) #不存在输入salary 42 while True: 43 print('商品清单'.center(40, '*')) #打印商品清单 44 for ii in goods: 45 print(ii, goods[ii]) 46 user_choice = input('which one do you like:') 47 if user_choice in goods.keys(): #判断用户输入的商品是否在merchant里,若在,则添加到shopping cart并扣费 48 shopping_cart.append(user_choice) 49 salary = salary - int(goods[user_choice]) 50 print('purchase success!') 51 elif user_choice == 'q': #‘q’退出并将shopping cart中的货物和余额打印出来,并将余额写入client的info中; 52 print('消费清单'.center(40,'*')) 53 for sh in shopping_cart: 54 print(sh+'\n') 55 print('you have \033[31;1m%s\033[0m left' % salary) 56 users_name[user_input]=salary 57 f1 = open('info_of_client.txt', 'w') 58 for mer in users_name: 59 print('{0}\t{1}'.format(mer, users_name[mer]), file=f1) 60 f1.close() 61 break 62 else: 63 print('the goods your input is none')
来一句励志的英文:
The world is like a mirror: Frown at it and it frowns at you; smile, and it smiles too.