# -*- coding:utf-8 -*-
import pickle
import time
import os
def cost(fname):
'用于记录花费'
cost_time = time.strftime('%Y-%m-%d')
try:#异常处理机制
cost_deposit = int(input('花销金额:'))
cost_mark = input('花销说明:')
except ValueError:
print('无效的金额')
return # 函数的return类似于循环的break,return提前结束函数。
except (KeyboardInterrupt, EOFError):
print('\nbye-bye')
exit(1)
# 在文件中取出所有的收支记录
with open(fname,'rb') as fobj:
records = pickle.load(fobj)
# 计算最新余额
balance = records[-1][-2] - cost_deposit
# 构建最新一笔收入
record = [cost_time,0,cost_deposit,balance,cost_mark]
# 将收入追加到收支列表中
records.append(record)
# 将最新收支情况写入文件
with open(fname,'wb') as fobj:
pickle.dump(records,fobj)
def save(fname):
save_time = time.strftime('%Y-%m-%d')
try:
save_deposit
Python小程序---记账
最新推荐文章于 2023-05-23 22:50:05 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)