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

Linda Hamilton
发布: 2024-10-17 06:15:02
原创
518 人浏览过

How I Built My First Python PET App (And What I Learned)

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

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

我面临的最重大挑战之一是弄清楚如何有效地存储费用数据。我最初在 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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!