用python写一个记账小程序_python实现日常记账本小程序

这篇博客介绍了如何使用Python创建一个简单的财务管理应用,包括存钱、花钱功能,并记录每笔交易详情。用户可以输入金额和消费用途,程序会更新钱包余额并保存到文件。此外,还提供查询功能,显示所有交易记录。此程序有待改进,如接受外部文件参数以优化代码结构。
摘要由CSDN通过智能技术生成

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、程序还有改进处,例如将两个文件以参数的形式传入,会简化代码。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

好的,以下是一个简单的记账本小程序的代码: ```python import datetime def add_income(income, income_dict): date = datetime.date.today().strftime('%Y-%m-%d') if date not in income_dict: income_dict[date] = {'income': income, 'expense': 0} else: income_dict[date]['income'] += income def add_expense(expense, expense_dict): date = datetime.date.today().strftime('%Y-%m-%d') if date not in expense_dict: expense_dict[date] = {'income': 0, 'expense': expense} else: expense_dict[date]['expense'] += expense def view_records(income_dict, expense_dict): print('日期\t\t收入\t\t支出') for date in income_dict.keys() | expense_dict.keys(): income = income_dict.get(date, {}).get('income', 0) expense = expense_dict.get(date, {}).get('expense', 0) print(f'{date}\t{income}\t\t{expense}') income_dict = {} expense_dict = {} while True: print('1. 添加收入') print('2. 添加支出') print('3. 查看记录') print('4. 退出') choice = input('请选择操作:') if choice == '1': income = float(input('请输入收入金额:')) add_income(income, income_dict) elif choice == '2': expense = float(input('请输入支出金额:')) add_expense(expense, expense_dict) elif choice == '3': view_records(income_dict, expense_dict) elif choice == '4': break else: print('无效的选择!') ``` 该程序可以实现基本的记账功能,包括添加收入、添加支出和查看记录。用户可以根据提示选择相应的操作,程序会根据用户的选择执行相应的操作,并输出记录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值