Python项目21:一个简单的记账系统(收入+支出+查询)

------------★Python练手项目源码★------------

Python项目源码20:银行管理系统(开户、查询、取款、存款、转账、锁定、解锁、退出)

Python项目19:学员信息管理系统(简易版)

Python项目18:使用Pillow模块,随机生成4位数的图片验证码

Python项目17:教你制作一副帅气的春联

Python项目16:教你使用pillow把女神的图片,添加表白文字。

Python项目15:Pygame制作,新年动态烟花

Python项目14:使用random,模拟扑克牌发牌+猜单词游戏

Python项目12:破解zip压缩包的密码

Python项目10:使用Tkinter批量新建文件夹

Python项目09:使用filestools模块,批量添加图片水印

Python项目08:用pywin32在聊天窗口发送QQ好友/群消息

Python小项目07:pywin32实现自动写文字到记事本

Python小项目:05模拟微信发送好友/群消息

Python小项目05:使用pywifi模块,暴力破解WIFI密码 !!亲测有效

Python经典小游戏02:字母数字代码雨

这是一个简单的记账程序,可以记录收入和支出,以及查询收支记录。程序运行时会首先检查数据文件account.data是否存在,如果不存在则创建并初始化数据。然后进入一个循环,等待用户输入并执行相应的功能,直到用户选择退出。注意:程序使用了pickle模块来序列化和反序列化数据,以便将数据保存在文件中。数据文件是一个二进制文件,包含了一个收支记录的列表,每个记录是一个包含日期、支出、收入、余额和说明的列表。程序的主要功能包括:

cost函数:记录支出,用户输入支出金额和说明,然后将记录追加到文件中。

save函数:记录收入,用户输入收入金额和说明,然后将记录追加到文件中。

query函数:查询收支记录,打印出所有记录。

keep_accounts函数:主程序,根据用户输入执行相应的功能,包括开销、收入、查询和退出。

python 复制代码
# -*- coding:utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
import pickle
import time
import os

# 1.记录开销
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 f:
        records = pickle.load(f)

    # 计算最新余额
    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)

# 2.收入
def save(fname):
    save_time = time.strftime('%Y-%m-%d')
    try:
        save_deposit = int(input('收入金额:'))
        save_mark = input('收入说明:')

    except ValueError:
        print('无效的金额')
        return

    except (KeyboardInterrupt, EOFError):
        print('bye-bye')
        exit(1)

    with open(fname, 'rb') as fobj:
        records = pickle.load(fobj)

    balance = records[-1][-2] + save_deposit
    record = [save_time, save_deposit, 0, balance, save_mark]
    records.append(record)
    with open(fname, 'wb') as fobj:
        pickle.dump(records, fobj)

# 3.查询
def query(fname):
    # 用于查账
    # 打印表头标题
    print(f'{"date":<15}{"save":<8}{"cost":<8}{"balance":<12}{"mark":<50}')
    with open(fname, 'rb') as f:
        records = pickle.load(f)
    for date, cost, save, balance, mark in records:
        print(f'{date:<15}{cost:<8}{save:<8}{balance:<12}{mark:<50}')

# 4.主程序
def keep_accounts():
    funcs = {'0': cost, '1': save, '2': query}
    prompt = '''*****************
(0)开销
(1)收入
(2)查询
(3)退出
*****************
请选择(0/1/2/3):'''

    fname = 'account.data'
    if not os.path.exists(fname):
        init_data = [[time.strftime('%Y-%m-%d'), 0, 0, 0, '默认']]
        with open(fname, 'wb') as f:
            pickle.dump(init_data, f)
    while True:
        try:
            choice = input(prompt).strip()
        except(KeyboardInterrupt, EOFError):
            choice = '3'
        if choice not in ['0', '1', '2', '3']:
            print('输入的数字无效,请重试')
            continue
        if choice == '3':
            print('已经退出,欢迎再次使用!')
            break
        # 执行相应的功能
        funcs[choice](fname)


keep_accounts()

完毕!!感谢您的收看

----------★★历史博文集合★★----------

我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具 NumPy Pygame

相关推荐
结衣结衣.3 分钟前
python中的函数介绍
java·c语言·开发语言·前端·笔记·python·学习
茫茫人海一粒沙6 分钟前
Python 代码编写规范
开发语言·python
林浩2337 分钟前
Python——异常处理机制
python
数据分析螺丝钉32 分钟前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
小蜗笔记1 小时前
在Python中实现多目标优化问题(7)模拟退火算法的调用
开发语言·python·模拟退火算法
TANGLONG2221 小时前
【C语言】数据在内存中的存储(万字解析)
java·c语言·c++·python·考研·面试·蓝桥杯
魏大橙2 小时前
Fastjson反序列化
开发语言·python
立黄昏粥可温2 小时前
Python 从入门到实战34(实例2:绘制蟒蛇)
开发语言·python
柚乐果果2 小时前
数据分析实战简例
java·大数据·python
luthane2 小时前
python 实现djb2哈希算法
python·算法·哈希算法