python编写一个会算账的脚本的示例代码
更新时间:2020年06月02日 14:46:39 作者:一个linux小白
这篇文章主要介绍了python编写一个会算账的脚本,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
python算账脚本
1.假如小明卡里有10000元去商场买东西发现钱不够又向父母借了5000账单如下
2.以下脚本就能实现上面的运算
from time import strftime import pickle import os try: def save(): data = strftime('\033[35m%Y-%m-%d\033[0m') money = int(input('How much do you have to save?:')) comment = input('Which come of money?') with open('account.book','rb') as fname: list = pickle.load(fname) record = list[-1][-2] balance = record + money list.append([data,money,0,balance,comment]) with open('account.book','wb') as fname: pickle.dump(list,fname) def cost(): data = strftime('\033[35m%Y-%m-%d\033[0m') money = int(input('How much did you spend?:')) comment = input('Where is it used?:') with open('account.book','rb') as fname: list = pickle.load(fname) record = list[-1][-2] balance = record - money list.append([data,0,money,balance,comment]) with open('account.book', 'wb') as fname: pickle.dump(list, fname) def query(): print('\033[34m%-20s%-9s%-9s%-10s%-18s\033[0m' % ('date','save','cost','balance','comment')) with open('account.book','rb') as fname: record = pickle.load(fname) for i in record: print('%-29s%-9s%-9s%-10s%-20s' % tuple(i)) def choice_memu(): promat=''' (0)save (1)cost (2)query (3)exit please choice:''' fname = 'account.book' if not os.path.exists(fname): with open(fname,'wb') as obj: t_t = strftime('\033[35m%Y-%m-%d\033[0m') data = [[t_t,0,0,10000,'int']] pickle.dump(data,obj) while 1: cmds = {'0':save,'1':cost,'2':query} choice = input(promat) if choice not in ['0','1','2','3']: continue if choice == '3': print('\033[32msee you\033[0m') break cmds[choice]() if __name__ == '__main__': choice_memu() except KeyboardInterrupt: print('\033[32msee you\033[0m') except ValueError: print('\033[31minvalid inputs\033[0m')
3.与上面的表格比较发现结果一样
总结
到此这篇关于python编写一个会算账的脚本的示例代码的文章就介绍到这了,更多相关python算账脚本内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
使用python计算方差方式——pandas.series.std()
这篇文章主要介绍了使用python计算方差方式——pandas.series.std(),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-05-05Python3 使用pip安装git并获取Yahoo金融数据的操作
这篇文章主要介绍了Python3 使用pip安装git并获取Yahoo金融数据的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-04-04
最新评论