Heim >Backend-Entwicklung >Python-Tutorial >Wie ich meine erste Python-PET-App erstellt habe (und was ich gelernt habe)

Wie ich meine erste Python-PET-App erstellt habe (und was ich gelernt habe)

Linda Hamilton
Linda HamiltonOriginal
2024-10-17 06:15:02631Durchsuche

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

Hello, DEV community! I'm Andre, a passionate beginner programmer diving into the world of Python. After wrestling with motivation for a few years, I decided to shift my focus to building real projects. Today, I want to share my story about creating my very first Python project: a Personal Expense Tracker (PET) app. (code at the end)

The Personal Expense Tracker is a command-line application designed to help users log their daily expenses, categorize them, and gain insights into their spending habits. My goal was to create a tool that empowers users to take control of their finances. (and mine! ahah)

One of the most significant challenges I faced was figuring out how to effectively store the expense data. I initially struggled with file handling in Python, but after some persistence, I finally implemented a working solution!

Through this project, I learned the importance of user input validation and ensuring that data is recorded consistently. I also gained valuable experience in managing files in Python to store and retrieve expense records.

Looking ahead, I plan to integrate data visualization features to help users see their spending patterns visually. Plus, I'm excited to implement a budgeting tool that allows users to set spending limits by category.

Completing the Personal Expense Tracker has been an incredibly rewarding experience, boosting my confidence as a developer. I’m eager to continue my learning journey into backend development and DevOps, with more projects on the horizon!

I’d love to hear your feedback! If you’ve built something similar or have tips for enhancing expense trackers, please share your insights!

`

def pet():
print("Welcome to PET!")
print("Your Personal Expense Tracker, helping you keep track of your expenses.")
print("Expenses Categories:")
print("[1] Food & Groceries")
print("[2] Transportation (Fuel, Public Transportation, etc...)")
print("[3] Utilities (Electricity, Water, Internet, etc...)")
print("[4] Entertainment & Leisure")
print("[5] Healthcare & Medical Expenses")
print("[6] Rent & Mortgage")
print("[7] Miscellaneous (for any uncategorized expenses)")

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}")

pet()

`

Das obige ist der detaillierte Inhalt vonWie ich meine erste Python-PET-App erstellt habe (und was ich gelernt habe). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn