首页 > 代码库 > 购物车程序
购物车程序
购物车程序:
1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录
#_*_coding:utf-8_*_ ‘‘‘ user_passwd 文件内容 {‘user01‘:‘123456‘,‘user02‘:‘123456‘,‘user03‘:‘123456‘} user_cart 文件内容 {‘user01‘: {"工资":‘‘, ‘余额‘:‘‘, ‘购物车‘: []},‘user02‘: {"工资":‘‘, ‘余额‘:‘‘, ‘购物车‘: []},‘user03‘: {"工资":‘‘, ‘余额‘:‘‘, ‘购物车‘: []},} product_list 文件内容(仅供打印) 货号:001 |商品名称:iphone 6 |价格:5888元 货号:002 |商品名称:Mac Pro |价格:12000元 货号:003 |商品名称:Coffer |价格:31元 货号:004 |商品名称:Python Alex |价格120元 product_price 文件内容 {‘001‘:‘5888‘,‘002‘:‘12000‘,‘003‘:‘31‘,‘004‘:‘120‘} ‘‘‘ import os from prettytable import PrettyTable def pretty_table_list(*arg): # 定义一个打印meun表的函数 goodslist = PrettyTable(["ID", "功能菜单"]) goodslist.align["功能菜单"] = "l" # l 左对齐 r右对齐,默认居中 goodslist.padding_width = 2 # 填充的宽度 [左右两边的像素] goodslist.add_row(["1", "查询商品列表"]) goodslist.add_row(["2", "查询购物车"]) goodslist.add_row(["3", "开始购物"]) goodslist.add_row(["4", "退出"]) print(goodslist) def check_user(): # 定义个用户登录模块,然并卵,挺low的 with open(‘user_passwd‘,‘r‘,encoding=‘utf-8‘) as f: a =f.readline() # 文件内的字符串存在形式{‘user01‘: ‘123456‘, ‘user02‘: ‘123456‘, ‘user03‘: ‘123456‘} username = eval(a) # 此时字符串已经成功字典 while True: user_name = input(‘输入用户名:\n‘) if user_name not in username: continue else: break keep_going = True while keep_going: userpasswd = input(‘输入{0}密码:\n‘.format(user_name)) if userpasswd == username[user_name]: return user_name elif len(userpasswd) ==0 or userpasswd != username[user_name]: a = input(‘按回车继续,或任意键退出。\n‘) if len(a) == 0: keep_going = True elif len(a) != 0: keep_going = False return False def print_product_list(*arg): # 定义一个打印商品列表的函数 with open(‘product_list‘,‘r‘,encoding=‘utf-8‘) as f: for line in f: print(line.strip()) def fetch_cart_list(name): # 定义个查看购物车的函数 with open(‘user_cart‘, ‘r‘, encoding=‘utf-8‘) as old_cart: for line in old_cart: cart = eval(line) # 将user_cart文件读出的内容,转换成字典给cart if len(cart[name][‘购物车‘]) == 0: print("购物车为空") # 033这个我还是不会用 else: print(cart[name][‘购物车‘]) pass def buy(name): # 定义一个购物车的函数 with open(‘user_cart‘,‘r‘,encoding=‘utf-8‘) as old_cart, open(‘product_price‘,‘r‘,encoding=‘utf-8‘) as p_p, open(‘product_name‘,‘r‘,encoding=‘utf-8‘) as p_n: # 打开老用户购物车文件,写一个新购物车文件 for line in old_cart: cart = eval(line) # 将user_cart文件读出的内容,转换成字典给cart for line in p_p: price = eval(line) # 将product_price文件读出的内容,转换成字典给price for line in p_n: goods = eval(line) # 将product_name文件读出的内容,转换成字典给goods # print(type(cart),cart) # print(type(price),price) # print(type(goods),goods) # print(type(cart[name][‘购物车‘]), cart[name][‘购物车‘]) keep_going = True while True: balance = input("输入预算金额:\n") # 用这个方法可以把用户输入非数字内容的报错消除,挺low的,但是可以用 try: balance = int(balance) break except: continue while keep_going: print_product_list() #调用读货物架的函数 cart_number = input("输入需要的物品编号:\n") if cart_number in goods: if balance - int(price[cart_number]) > 0: a = goods[cart_number] cart[name][‘购物车‘].append(a) print(a) balance = balance - int(price[cart_number]) kp = input("继续购买按任意键,或按q退出") if kp != ‘q‘ or len(kp) == 0: keep_going = True else: keep_going = False elif balance - int(price[cart_number]) < 0: print("余额不足,请重新输入。\n") print(cart[name][‘购物车‘]) kp = input("继续购买按任意键,或按q退出") if kp != ‘q‘ or len(kp) == 0: keep_going = True else: keep_going = False with open(‘user_cart-new‘, ‘w‘, encoding=‘utf-8‘) as new_cart: new_cart.write(str(cart)) os.rename(‘user_cart‘,‘user_cart_bk‘) os.rename(‘user_cart-new‘,‘user_cart‘) os.remove(‘user_cart_bk‘) def main(): # 程序开始的地方 name = check_user() while True: pretty_table_list() meun_dic = { ‘1‘: print_product_list, ‘2‘: fetch_cart_list, ‘3‘: buy, ‘4‘: exit } choice = input(">>>输入操作编号").strip() if len(choice) == 0 or choice not in meun_dic: continue elif choice == 4: break else: meun_dic[choice](name) if __name__ == "__main__": main()
购物车程序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。