我如何构建我的第一个 Python PET 应用程序(以及我学到的东西)

碧海醫心
发布: 2024-10-17 08:43:00
转载
386人浏览过

我如何构建我的第一个 python pet 应用程序(以及我学到的东西)

您好,dev 社区!我是 andre,一位热衷于深入 python 世界的初学者程序员。在与动力斗争了几年之后,我决定将注意力转移到构建真正的项目上。今天,我想分享关于创建我的第一个 python 项目的故事:个人支出跟踪器 (pet) 应用程序。 (代码在最后)

个人开支跟踪器是一款命令行应用程序,旨在帮助用户记录日常开支、对其进行分类并深入了解他们的消费习惯。我的目标是创建一种工具,使用户能够控制自己的财务。 (还有我的!啊哈)

我面临的最重大挑战之一是弄清楚如何有效地存储费用数据。我最初在 python 中处理文件时遇到了困难,但经过一番坚持,我终于实现了一个可行的解决方案!

通过这个项目,我了解了用户输入验证和确保数据记录一致的重要性。我还获得了使用 python 管理文件以存储和检索费用记录的宝贵经验。

立即学习Python免费学习笔记(深入)”;

展望未来,我计划集成数据可视化功能,帮助用户直观地看到他们的消费模式。另外,我很高兴能够实现一个预算工具,允许用户按类别设置支出限额。

完成个人支出跟踪器是一次非常有益的经历,增强了我作为开发人员的信心。我渴望继续我的后端开发和 devops 学习之旅,更多项目即将到来!

我很想听听您的反馈!如果您构建了类似的东西或有增强费用跟踪器的技巧,请分享您的见解!

`

def pet():
print(“欢迎来到 pet!”)
print(“您的个人开支跟踪器,帮助您跟踪您的开支。”)
print("费用类别:")
print("[1] 食品和杂货")
print("[2] 交通(燃油、公共交通等...)")
print("[3] 公用事业(电力、水、互联网等...)")
print("[4] 娱乐休闲")
print("[5] 医疗保健和医疗费用")
print("[6] 租金和抵押贷款")
print("[7] 杂项(任何未分类的费用)")

categories = [
    "Food & Groceries",
    "Transportation (Fuel, Public Transportation, etc...)",
    "Utilities (Electricity, Water, Internet, etc...)",
    "Entertainment & Leisure",
    "Healthcare & Medical Expenses",
    "Rent & Mortgage",
    "Miscellaneous (for any uncategorized expenses)"
]

food = []
transportation = []
utilities = []
entertainment = []
healthcare = []
rent = []
miscs = []

while True:
    while True:
        try:
            choice = int(input("Select category: "))
            if 1 <= choice <= 7:
                break
            else:
                raise ValueError
        except ValueError:
            return "Choose a valid category!"

    while True:
            try:
                amount = float(input("Amount: "))
                break
            except ValueError:
                return "Invalid number! Enter the amount of the expense."

    if choice == 1:
        food.append(amount)
        print(f"${amount} added to {categories[0]}")
    elif choice == 2:
        transportation.append(amount)
        print(f"${amount} added to {categories[1]}")
    elif choice == 3:
        utilities.append(amount)
        print(f"${amount} added to {categories[2]}")
    elif choice == 4:
        entertainment.append(amount)
        print(f"${amount} added to {categories[3]}")
    elif choice == 5:
        healthcare.append(amount)
        print(f"${amount} added to {categories[4]}")
    elif choice == 6:
        rent.append(amount)
        print(f"${amount} added to {categories[5]}")
    elif choice == 7:
        miscs.append(amount)
        print(f"${amount} added to {categories[6]}")

    option = input("Do you want to add another expense? (Y/N)").lower()

    if option != 'y':
        break
    else: 
        continue

food_total = sum(food)
transportation_total = sum(transportation)
utilities_total = sum(utilities)
entertainment_total = sum(entertainment)
healthcare_total = sum(healthcare)
rent_total = sum(rent)
miscs_total = sum(miscs)

print("Options:")
print("[1] View total spent")
print("[2] View total per category")

while True:
    try:
        show_expenses = int(input("Choose an option: "))
        if 1 <= show_expenses <= 2:
            break
        else:
            raise ValueError
    except ValueError:
        return "Invalid! Insert a valid option."

if show_expenses == 1:
    total_expenses = food_total + transportation_total + utilities_total + entertainment_total + healthcare_total + rent_total + miscs_total
    print(f"Your total expenses: ${total_expenses}")
elif show_expenses == 2:
    print(f"{categories[0]} total is: ${food_total}")
    print(f"{categories[1]} total is: ${transportation_total}")
    print(f"{categories[2]} total is: ${utilities_total}")
    print(f"{categories[3]} total is: ${entertainment_total}")
    print(f"{categories[4]} total is: ${healthcare_total}")
    print(f"{categories[5]} total is: ${rent_total}")
    print(f"{categories[6]} total is: ${miscs_total}")
登录后复制

宠物()

`

以上就是我如何构建我的第一个 Python PET 应用程序(以及我学到的东西)的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号