shopping_car()
from atm.lib_common.file_handle import * from atm.core.shopping import goods_show from atm.lib_common.money_enquiry import * def compute_money_total(username): """计算购物车总价 返回购物车总价和购物车字典 """ goods_dict=dict() money_total=0 print("\033[0;33;40m", "欢迎来到购物车功能", "\033[0m") goods_list=file_r(r"F:\pylearn\atm\api\购物车列表.txt") for i in goods_list: goodsname=i.strip().split(":")[1] # 商品名称 goodsnum=i.strip().split(":")[2]# 数量 goodsmoney=i.strip().split(":")[3] # 小计 goods_dict[goodsname]=[goodsnum,goodsmoney] #print(goods_dict) for i in goods_dict: if goods_dict[i][1].isdigit(): goods_money_int=int(goods_dict[i][1]) #print(goods_money_int) money_total=money_total+goods_money_int #print(money_total) return money_total,goods_dict def modify_shopping_car(username): """修改购物车商品以及数量""" money_total,goods_dict=compute_money_total("黄健") print("\033[0;33;40m", "现有购物车列表如下", "\033[0m") print(goods_dict) print(f"总共需要支付{money_total}") money_enquiry(username) goods_np_dict=goods_show() #print(goods_np_dict) modify_target=input("请输入你想修改的商品名称:>>>") modify_num=input(f"你想将{modify_target}的数量更改为:>>>") goods_dict[modify_target][0]=modify_num#修改数量 goods_dict[modify_target][1]=int(goods_np_dict[modify_target])*int(modify_num)#修改小计 print("\033[0;33;40m", "修改成功!修改后购物车列表如下", "\033[0m") print(goods_dict) file_w(r"F:\pylearn\atm\api\购物车列表.txt", "用户名:商品名称:数量:小计\n") with open(r"F:\pylearn\atm\api\购物车列表.txt","a",encoding="utf8") as fa: for i in goods_dict: if i !="商品名称": fa.write(f"{username}:{i}:{goods_dict[i][0]}:{goods_dict[i][1]}\n") money_total,goods_dict=compute_money_total("黄健") print(f"修改后需要支付{money_total}")
标签:goods,shopping,money,atm,day21,购物车,dict,print,total From: https://www.cnblogs.com/yyyjw/p/17898919.html