python实现一个记账系统_Python实现简单的记账本功能
#!/usr/bin/env python#-*- coding: utf-8 -*-
importos,timeimportcPickle as pdefsave_money(wallet, record, amount, comment):
date= time.strftime("%Y-%m-%d")
with open(wallet) as fobj:
balance= p.load(fobj) +amount
with open(wallet,wb) as fobj:
p.dump(balance, fobj)
with open(record,a) as fobj:
fobj.write("%-12s%-8s%-8s%-10s%-20s" %(
date,N/A, amount, balance, comment
)
)defcost_money(wallet, record, amount, comment):
date= time.strftime("%Y-%m-%d")
with open(wallet) as fobj:
balance= p.load(fobj) -amountif balance <0:print "余额不足,请先存钱或进行其他操作!"
else:
with open(wallet,wb) as fobj:
p.dump(balance, fobj)
with open(record,a) as fobj:
fobj.write("%-12s%-8s%-8s%-10s%-20s" %(
date, amount,N/A, balance, comment
)
)defquery_money(wallet, record):print "%-12s%-8s%-8s%-10s%-20s" %(date, cost, save, balance, comment)
with open(record) as fobj:for line infobj:printline,
with open(wallet) as fobj:print "New Balance:
%s" %p.load(fobj)defshow_menu():
w_file= wallet.datar_file= record.txtcmds={0: save_money,1: cost_money,2: query_money
}
prompt= """(0) save money
(1) spend money
(2) query detail
(3) quit
Please input your choice(0/1/2/3):"""
if notos.path.isfile(w_file):
with open(w_file,w) as fobj:
p.dump(0, fobj)if notos.path.isfile(r_file):
os.mknod(r_file)whileTrue:
args=(w_file, r_file)
choice=raw_input(prompt).strip()[0]if choice not in 0123:print "Invalid input, Try again."
continue
if choice in 01:
amount= int(raw_input("Amount:"))
comment= raw_input("Comment:")
args=(w_file, r_file, amount, comment)if choice == 3:breakcmds[choice](*args)if __name__ == __main__:print show_menu()
#!/usr/bin/env python#-*- coding: utf-8 -*- importos,timeimportcPickle as pdefsave_money(wallet, record, amount, comment): date= time.strftime("%Y-%m-%d") with open(wallet) as fobj: balance= p.load(fobj) +amount with open(wallet,wb) as fobj: p.dump(balance, fobj) with open(record,a) as fobj: fobj.write("%-12s%-8s%-8s%-10s%-20s" %( date,N/A, amount, balance, comment ) )defcost_money(wallet, record, amount, comment): date= time.strftime("%Y-%m-%d") with open(wallet) as fobj: balance= p.load(fobj) -amountif balance <0:print "余额不足,请先存钱或进行其他操作!" else: with open(wallet,wb) as fobj: p.dump(balance, fobj) with open(record,a) as fobj: fobj.write("%-12s%-8s%-8s%-10s%-20s" %( date, amount,N/A, balance, comment ) )defquery_money(wallet, record):print "%-12s%-8s%-8s%-10s%-20s" %(date, cost, save, balance, comment) with open(record) as fobj:for line infobj:printline, with open(wallet) as fobj:print "New Balance: %s" %p.load(fobj)defshow_menu(): w_file= wallet.datar_file= record.txtcmds={0: save_money,1: cost_money,2: query_money } prompt= """(0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3):""" if notos.path.isfile(w_file): with open(w_file,w) as fobj: p.dump(0, fobj)if notos.path.isfile(r_file): os.mknod(r_file)whileTrue: args=(w_file, r_file) choice=raw_input(prompt).strip()[0]if choice not in 0123:print "Invalid input, Try again." continue if choice in 01: amount= int(raw_input("Amount:")) comment= raw_input("Comment:") args=(w_file, r_file, amount, comment)if choice == 3:breakcmds[choice](*args)if __name__ == __main__:print show_menu()