from atm.lib_common.file_handle import * from atm.lib_common.moner_enquiry import * def goods_show(): """商品名称及价格获取""" goods=file_r(r"F:\pylearn\atm\api\商品列表.txt") #print(goods) goodslist_len=len(goods) #print(goodslist_len) goods_name_list=list() goods_price_list=list() goods_np_dict=dict() for i in range(goodslist_len): goods_name_list.append(goods[i].strip().split(":")[0])#商品名称列表 goods_price_list.append(goods[i].strip().split(":")[1])#商品价格列表 goods_np_dict[goods[i].strip().split(":")[0]]=goods[i].strip().split(":")[1]#名称:价格字典 return goods_np_dict def shopping(username): """购物""" print("\033[0;33;40m", "欢迎来到购物功能", "\033[0m") file_w(r"F:\pylearn\atm\api\购物车列表.txt", "用户名:商品名称:数量:小计\n") goods=goods_show()#商品名称价格字典 print("下面是商品列表\n",goods) shopping_flag=True while shopping_flag: goods_choice=input(f"尊敬的用户{username}请输入你想要购买的商品名称! 按q退出选购:>>>>") if goods_choice=="q": print("欢迎下次再来!") return elif goods_choice not in goods: print("您输入的商品不存在!") continue else: goods_number=input("请选择您购买的数量:>>>") print(f"尊敬的用户{username}您好,以下是您的购物信息:>>>") print(f"商品名称:{goods_choice},商品单价:{goods[goods_choice]},购买数量:{goods_number}") total_price=int(goods[goods_choice])*int(goods_number) print(f"总价为:{total_price}") adding_choice=input("是否加入购物车,确认输入y,取消输入n:>>>") if adding_choice=="y": file_a(r"F:\pylearn\atm\api\购物车列表.txt",f"{username}:{goods_choice}:{goods_number}:{total_price} \n") print("\033[0;31;40m恭喜您!已成功加入购物车!\033[0m") shopping_again_1=input("是否继续购物?继续购物请按y,退出请按q:>>>") if shopping_again_1=="y": print("*" * 120) print("请继续选购!") elif shopping_again_1=="q": print("欢迎下次再来!") return elif adding_choice=="n": print("取消成功!") shopping_again_2 = input("是否继续购物?继续购物请按y,退出请按q:>>>") if shopping_again_2 == "y": print("*" * 120) print("请继续选购!") elif shopping_again_2 == "q": print("欢迎下次再来!") return shopping("黄健")
标签:goods,shopping,atm,list,choice,print,day19 From: https://www.cnblogs.com/yyyjw/p/17893305.html