1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 salary=input("请输入工资: ") 5 if salary.isdigit(): 6 salary=int(salary) 7 price_recard=[] 8 price_recard=[salary] 9 while True: 10 f = open("shop_list", "r") 11 lines=f.readlines() 12 print("-----------商品列表-----------") 13 for line in lines: 14 print(line) 15 num=input("请输入购买商品的编号 余额查询:p 购物记录:w >>>") 16 if num.isdigit(): 17 num=int(num) 18 if num <=int(len(lines)) and num >0: 19 for line in lines: 20 if len(line)==0:break 21 if num == int(line.split()[0]): 22 price=int(line.split()[2]) 23 product=line.split()[1] 24 if salary >=price: 25 salary -=price 26 s=open("recard.txt","a") 27 s.write("添加商品%s %s元的到你的购物车\n" % (product,price)) 28 s.flush() 29 print("添加商品%s %s元的到你的购物车,账号余额%s元" % (product,price,salary)) 30 f.close() 31 price_recard=[salary] 32 else: 33 print("余额不足!当前余额为%s" %(salary)) 34 f.close() 35 else: 36 print("商品不存在") 37 f.close() 38 elif num=="q": 39 n=open("recard.txt","r+") 40 lines=n.readlines() 41 while True: 42 for line in lines: 43 #if len(line)==0:break 44 print(line) 45 break 46 n.close() 47 f.close() 48 n = open("recard.txt", "w") 49 n.truncate(); 50 n.close() 51 for i in price_recard: 52 #if len(i)==0:break 53 print("您账户余额为%s元" %i) 54 f=open("recard.txt","w+") 55 f.close() 56 print("----再见----") 57 exit() 58 elif num=="w": 59 n=open("recard.txt","r") 60 lines=n.readlines() 61 while True: 62 for line in lines: 63 if len(line)==0:break 64 print(line) 65 break 66 67 elif num=="p": 68 for i in price_recard: 69 print("您账户余额为%s元" % i)
时间: 2024-10-28 17:34:36