python實(shí)現(xiàn)日常記賬本小程序
python實(shí)現(xiàn)收支的自動(dòng)計(jì)算,能夠查詢每筆賬款的消費(fèi)詳情,具體內(nèi)容如下
1、函數(shù)需要兩個(gè)文件:一個(gè)類似錢包功能,存放錢;另一個(gè)用于記錄每筆花銷的用途
#!/usr/bin/env python
import cPickle as p
with open('wallet.data','w') as f:
p.dump(10000,f)
with open('record.txt','w') as f:
pass
2、功能實(shí)現(xiàn)
#!!/usr/bin/env python
#coding:utf8
import cPickle as p
import time
date = time.strftime('%Y%m%d')
def save_money():
sav_count=int(raw_input('save money: '))
sav_comment = raw_input('doing what: ')
with open('wallet.data') as f:
balance = p.load(f)
new_bal = balance + sav_count
with open('wallet.data','w') as f:
p.dump(new_bal,f)
content = '%-12s%-8s%-8s%-10s%-25s\n'%(date,'N/A',sav_count,new_bal,sav_comment)
with open('record.txt','a')as f:
f.write(content)
def spend_money():
spe_count=int(raw_input('spend money: '))
spe_comment = raw_input('doing what: ')
with open('wallet.data') as f:
balance = p.load(f)
new_bal = balance - spe_count
with open('wallet.data','w') as f:
p.dump(new_bal,f)
with open('record.txt','a')as f:
content = '%-12s%-8s%-8s%-10s%-25s\n'%(date,spe_count,'N/A',new_bal,spe_comment)
f.write(content)
def query_info():
line = '='*63
content = '%s\n%-12s%-8s%-8s%-10s%-25s'%(line,'Date','Cost','Save','Balance','Comment')
with open('wallet.data') as f:
new_bal = p.load(f)
print 'new balance: ',new_bal
print content
with open('record.txt') as f:
for line in f:
print line
def show_menu():
prompt = '''''
'0':'spend_money'
'1':'save_money'
'2':'query_info'
'3':'quit'
'''
while True:
CMDs={'0':spend_money,'1':save_money,'2':query_info}
choice = raw_input('which do you want to do ?%s: '%prompt)
if choice not in '012':
break
CMDs[choice]()
if __name__=='__main__':
show_menu()
3、程序還有改進(jìn)處,例如將兩個(gè)文件以參數(shù)的形式傳入,會(huì)簡(jiǎn)化代碼。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
matplotlib bar()實(shí)現(xiàn)百分比堆積柱狀圖
這篇文章主要介紹了matplotlib bar()實(shí)現(xiàn)百分比堆積柱狀圖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
基于Tensorflow讀取MNIST數(shù)據(jù)集時(shí)網(wǎng)絡(luò)超時(shí)的解決方式
這篇文章主要介紹了基于Tensorflow讀取MNIST數(shù)據(jù)集時(shí)網(wǎng)絡(luò)超時(shí)的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
基于Python實(shí)現(xiàn)的微信好友數(shù)據(jù)分析
這篇文章主要介紹了基于Python實(shí)現(xiàn)的微信好友數(shù)據(jù)分析的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Pycharm中無(wú)法使用pip安裝的包問(wèn)題解決方案
本文主要介紹了Pycharm中無(wú)法使用pip安裝的包問(wèn)題解決方案,在終端通過(guò)pip裝好包以后,在pycharm中導(dǎo)入包時(shí),依然會(huì)報(bào)錯(cuò),下面就來(lái)介紹一下解決方法2023-09-09
利用Python如何將數(shù)據(jù)寫到CSV文件中
在數(shù)據(jù)分析中經(jīng)常需要從csv格式的文件中存取數(shù)據(jù)以及將數(shù)據(jù)寫書到csv文件中。下面這篇文章主要給大家介紹了關(guān)于利用Python如何將數(shù)據(jù)寫到CSV文件中的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-06-06
解決Python下json.loads()中文字符出錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決Python下json.loads()中文字符出錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

