国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現日常記賬本小程序

2020-02-22 23:24:50
字體:
來源:轉載
供稿:網友

python實現收支的自動計算,能夠查詢每筆賬款的消費詳情,具體內容如下

1、函數需要兩個文件:一個類似錢包功能,存放錢;另一個用于記錄每筆花銷的用途

#!/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、功能實現

#!!/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、程序還有改進處,例如將兩個文件以參數的形式傳入,會簡化代碼。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 竹山县| 五家渠市| 龙胜| 柞水县| 辽宁省| 仙居县| 兴和县| 辉南县| 呼伦贝尔市| 铜鼓县| 长垣县| 鄂温| 广州市| 辽中县| 龙门县| 娱乐| 吉林省| 于田县| 和田县| 鹤岗市| 青河县| 英吉沙县| 阿克苏市| 陕西省| 济阳县| 济宁市| 武陟县| 常德市| 海丰县| 岳西县| 甘孜| 仁化县| 望奎县| 威远县| 山西省| 宁陵县| 赫章县| 康乐县| 亳州市| 兴安县| 固镇县|