#Author:huaiq #Author:huaiq shopping_dict ={} Shopping_car =[] while True: Money = input("请输入您所携带人民币金额:") if Money.isdigit(): Money = int(Money) break else: print("你的输入不规范,请重新输入!") while True: print("-----商品价目表及其编号-----") Commodity = open(r"D:\360Downloads\shopping.txt",'r') for user_data in Commodity: user_list = user_data.strip() user_datas = user_list.split(',') Commodity_name = user_datas[0].strip() Commodity_price = int(user_datas[1].strip()) shopping_dict[Commodity_name]= {'Commodity_name': Commodity_name, 'Commodity_price': Commodity_price} Commodity.close() for key in shopping_dict : print( '{0:10} : {1:10d}'.format(key,shopping_dict[key]['Commodity_price'],chr(12288))) shopping_name = input("请选择您心仪的商品:").strip() if shopping_name in shopping_dict.keys(): if shopping_dict[shopping_name]['Commodity_price'] <= Money: Money -= shopping_dict[shopping_name]['Commodity_price'] Shopping_car.append(shopping_name) print("你已经成功购买该产品!您的余额为\033[32;1m%s\033[0m!"%(Money)) else: print("您的余额不足以支付该金额!您的余额为\033[31;1m%s\033[0m!"%(Money)) elif shopping_name == 'q': print("\033[33;1m您已经成功退出,欢迎下次光临\033[0m!!") print("-----已购清单-----") print(Shopping_car) print("您的余额为\033[31;1m%s\033[0m!" % (Money)) exit() else: print("该商品不存在!请重新选择或退出")
1.关于中英文混搭的输出格式太麻烦了没写,如文中的Ipone数据线
2.字典的打印弄了好久以及格式还是没有完全解决