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

相关推荐
AI探索者7 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者7 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh9 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅9 小时前
Python函数入门详解(定义+调用+参数)
python
曲幽10 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时13 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿16 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780511 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python