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

相关推荐
tryCbest14 小时前
Python基础之爬虫技术(一)
开发语言·爬虫·python
husterlichf14 小时前
逻辑回归以及python(sklearn)详解
python·逻辑回归·sklearn
AI小云14 小时前
【Numpy数据运算】数组间运算
开发语言·python·numpy
q***783714 小时前
【玩转全栈】----Django制作部门管理页面
后端·python·django
CoderIsArt15 小时前
抽象语法树AST与python的Demo实现
python
郝开16 小时前
Spring Boot 2.7.18(最终 2.x 系列版本)3 - 枚举规范定义:定义基础枚举接口;定义枚举工具类;示例枚举
spring boot·后端·python·枚举·enum
钅日 勿 XiName17 小时前
一小时速通Pytorch之自动梯度(Autograd)和计算图(Computational Graph)(二)
人工智能·pytorch·python
故林丶17 小时前
【Django】Django笔记
python·django
IT北辰17 小时前
Python实现居民供暖中暖气能耗数据可视化分析(文中含源码)
开发语言·python·信息可视化
FreeCode17 小时前
LangChain1.0智能体开发:长期记忆
python·langchain·agent