#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: liang #!/usr/bin/env python # -*- coding:utf-8 -*- # Author: liang import sys,os,sys commodity_list=[ ('IPhone',5600), ('Max Pro',1200), ('book',120), ('Girl friend',2800), ('English',400) ] shopping_list=[] f3=open('ming.txt','a',encoding="utf-8") f=open("salary.txt",'a',encoding="utf-8") f2=open("salary.txt",'r+',encoding="utf-8") salary=input("请输入你的工资:") if salary.isdigit(): salary=int(salary) while True: for index, item in enumerate(commodity_list): print(index,item) user_choie=input("你需要购买什么?:") if user_choie.isdigit(): user_choie=int(user_choie) if user_choie < len(commodity_list) and user_choie >=0: p_item=commodity_list[user_choie] if p_item[1] <= salary: salary -= p_item[1] p_item = str(p_item) f.write(p_item + ' ') f.flush() a=("你购买的商品是%s,你的余额还有%s"%(p_item,salary)) print(a) f3.writelines(a+' ') else: print("你的余额只有%s,那你还买个屁啊!!"%(salary)) else: print("尚品不存在") elif user_choie == 'q': print("----------shopping list---------") print(f2.read()) exit("你的余额为%s"%(salary)) else: print("你输入的有误!!。请重新输入!!!") else: print("你输入的有误!!请从新输入")
ming.txt
你购买的商品是('book', 120),你的余额还有39880 你购买的商品是('Girl friend', 2800),你的余额还有37080 你购买的商品是('English', 400),你的余额还有36680
salary.txt
('book', 120) ('Girl friend', 2800) ('English', 400)