加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python实现日常记账本小程序

发布时间:2020-12-17 07:22:23 所属栏目:Python 来源:网络整理
导读:python实现收支的自动计算,能够查询每笔账款的消费详情,具体内容如下 1、函数需要两个文件:一个类似钱包功能,存放钱;另一个用于记录每笔花销的用途 #!/usr/bin/env python import cPickle as p with open('wallet.data','w') as f: p.dump(10000,f) wit

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%-25sn'%(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',f) 
 
  with open('record.txt','a')as f: 
    content = '%-12s%-8s%-8s%-10s%-25sn'%(date,spe_count,spe_comment) 
    f.write(content) 
 
def query_info(): 
  line = '='*63 
  content = '%sn%-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、程序还有改进处,例如将两个文件以参数的形式传入,会简化代码。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • 微信小程序跳一跳游戏 python脚本跳一跳刷高分技巧
  • Python实现的检测web服务器健康状况的小程序
  • 一个计算身份证号码校验位的Python小程序
  • Python实现的一个找零钱的小程序代码分享
  • python写的一个squid访问日志分析的小程序
  • 小米5s微信跳一跳小程序python源码
  • Python实现比较扑克牌大小程序代码示例

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读