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

相关推荐
Li emily2 小时前
解决了加密货币api多币种订阅时的数据乱序问题
人工智能·python·api·fastapi
2301_781571422 小时前
Golang格式化输出占位符都有什么_Golang fmt占位符教程【通俗】
jvm·数据库·python
asdzx672 小时前
使用 Python 为 PDF 添加页码 (详细教程)
python·pdf·页码
AI技术控3 小时前
《Transformers are Inherently Succinct》论文解读:从“能表达什么”到“多紧凑地表达”
人工智能·python·深度学习·机器学习·自然语言处理
金融大 k5 小时前
Python 全球指数监控面板:TickDB + REST + WebSocket 完整方案
python·websocket
啊哈哈121385 小时前
系统设计复盘:为什么 Agent 的 ReAct 循环必须内嵌确定性保护层——以 FitMind 健康助手的路由与步骤控制为例
人工智能·python·react
一颗牙牙6 小时前
安装mmcv
开发语言·python·深度学习
大数据魔法师6 小时前
Streamlit(二)- Streamlit 架构与运行机制
python·web
m0_470857646 小时前
PHP怎么实现工厂模式_Factory模式编写指南【指南】
jvm·数据库·python
大数据魔法师7 小时前
Streamlit(三)- Streamlit 多页面应用开发
python·web