1 __author__ = ‘ZZG‘ 2 3 product_list =[ 4 ["iphone",5300], 5 ["book",55], 6 ["watch",2200], 7 ["bike",950], 8 ["pc",11000], 9 ["VR glass", 1000] 10 ] 11 #将产品列表写入文件中 12 with open(‘product_list.txt‘,‘w‘)as pro: 13 for i in product_list: 14 pro.write(str(i)) 15 pro.write("\n") 16 pro.close() 17 entrance = input("选择用户入口还是商家入口:") 18 exit_flag = True 19 shopping_list = [] 20 21 #判断入口 22 if entrance == "用户入口": 23 salary = input("请输入你的工资:") 24 25 while exit_flag: 26 with open("salary.txt",‘w+‘)as sa: 27 sa.write(str(salary)) 28 sa.close() 29 #判断工资是否为数字 30 if str(salary).isdigit(): 31 salary = int(salary) 32 if salary > 0: 33 #读取并答应文件中的商品列表 34 with open(‘product_list.txt‘)as p: 35 for index ,products in enumerate(p): 36 print(index,products) 37 #用户输入购买商品的序号 38 number_of_product = input("\n请输入商品的序号") 39 #判断输入的类型 40 if number_of_product.isdigit(): 41 number_of_product = int(number_of_product) 42 #判断输入的商品序号是否存在 43 44 if number_of_product < index+1 and number_of_product >= 0: 45 products = product_list[number_of_product] 46 47 if salary > products[1]: 48 salary = salary - products[1] 49 shopping_list.append(products) 50 print(‘已购买商品列表‘.center(50,‘-‘)) 51 for i1 in shopping_list: 52 print(i1) 53 print("余额为\033[032;1m%s\033[0m"%( salary)) 54 with open(‘shoping_list.txt‘,‘a+‘)as sl: 55 sl.write(str(shopping_list)) 56 sl.close() 57 continue 58 else: 59 print("你的余额为\033[031;1m%s\033[0m,买不了该商品\n"%(salary)) 60 continue 61 else: 62 print("\033[031;1m你输入的商品不存在\033[0m") 63 for i in shopping_list: 64 pass 65 print("你已经购买了\n\033[021;1m%s\n\033[0m余额为\033[032;1m%s\033[0m"%(i, salary)) 66 67 continue 68 elif number_of_product == "退出": 69 print(‘已购买商品列表‘.center(50,‘-‘)) 70 for i in shopping_list: 71 print(i) 72 print("余额为\033[032;1m%s\033[0m"%( salary)) 73 exit() 74 else: 75 print("\033[031;1m不合法输入,按提示重新输入\033[0m") 76 break 77 78 else: 79 print("\033[031;1m不合法输入,按提示重新输入\033[0m") 80 else: 81 print("\033[031;1m不合法输入,按提示重新输入\033[0m") 82 exit_flag = False 83 84 elif entrance =="商家入口": 85 pass 86 else: 87 print("\033[031;1m不合法输入,按提示重新输入\033[0m")
时间: 2024-10-24 22:55:46