python实现日常记账本小程序
python实现收支的自动计算,能够查询每笔账款的消费详情,具体内容如下
1、函数需要两个文件:一个类似钱包功能,存放钱;另一个用于记录每笔花销的用途
#!/usr/bin/envpython importcPickleasp withopen('wallet.data','w')asf: p.dump(10000,f) withopen('record.txt','w')asf: pass
2、功能实现
#!!/usr/bin/envpython #coding:utf8 importcPickleasp importtime date=time.strftime('%Y%m%d') defsave_money(): sav_count=int(raw_input('savemoney:')) sav_comment=raw_input('doingwhat:') withopen('wallet.data')asf: balance=p.load(f) new_bal=balance+sav_count withopen('wallet.data','w')asf: p.dump(new_bal,f) content='%-12s%-8s%-8s%-10s%-25s\n'%(date,'N/A',sav_count,new_bal,sav_comment) withopen('record.txt','a')asf: f.write(content) defspend_money(): spe_count=int(raw_input('spendmoney:')) spe_comment=raw_input('doingwhat:') withopen('wallet.data')asf: balance=p.load(f) new_bal=balance-spe_count withopen('wallet.data','w')asf: p.dump(new_bal,f) withopen('record.txt','a')asf: content='%-12s%-8s%-8s%-10s%-25s\n'%(date,spe_count,'N/A',new_bal,spe_comment) f.write(content) defquery_info(): line='='*63 content='%s\n%-12s%-8s%-8s%-10s%-25s'%(line,'Date','Cost','Save','Balance','Comment') withopen('wallet.data')asf: new_bal=p.load(f) print'newbalance:',new_bal printcontent withopen('record.txt')asf: forlineinf: printline defshow_menu(): prompt=''''' '0':'spend_money' '1':'save_money' '2':'query_info' '3':'quit' ''' whileTrue: CMDs={'0':spend_money,'1':save_money,'2':query_info} choice=raw_input('whichdoyouwanttodo?%s:'%prompt) ifchoicenotin'012': break CMDs[choice]() if__name__=='__main__': show_menu()
3、程序还有改进处,例如将两个文件以参数的形式传入,会简化代码。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。