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

相关推荐
myenjoy_123 分钟前
MQTT 与 Sparkplug B——从车间到云端的最后一公里
网络·python
颜酱2 小时前
LangChain 输出解析器:把模型回复变成你要的数据
python·langchain
2401_873479402 小时前
企业安全运营中,如何用IP离线库提前发现失陷主机?三步实现风险画像
网络·数据库·python·tcp/ip·ip
weixin_523185322 小时前
Java基础知识总结(四):引用数据类型与参数传递机制
java·开发语言·python
码农飞哥3 小时前
我把RAG召回率从60%提到90%,就改了这两件事
python·知识库·向量检索·rag·效果提示
宸津-代码粉碎机3 小时前
Spring AI企业级实战|从RAG优化到Agent多工具调度
java·大数据·人工智能·后端·python·spring
yuhuofei20213 小时前
【Python入门】Python中的字典dict
python
Jinkxs3 小时前
Python基础 - 文件的写入操作 write与writelines方法
android·服务器·python
初学Python的小明3 小时前
Python格式化输出、运算符、分支&循环
python
92year3 小时前
用 browser-use 让 AI 自己操作浏览器:从安装到自动填表全流程
python·ai·浏览器自动化·browser-use