#i need you to suggest and correct my mistakes will help me lot

1 messages · Page 1 of 1 (latest)

dense sapphire
#

class ExpenseTracker:
def init(self):
self.expenses = {}
self.categories = {}

def add_expense(self, amount, category):
    if category not in self.categories:
        self.categories[category] = 0
    self.categories[category] += amount

    if category not in self.expenses:
        self.expenses[category] = []
    self.expenses[category].append(amount)

def show_summary(self):
    print("Expense Summary:")
    for category, amount_list in self.expenses.items():
        total_amount = sum(amount_list)
        print(f"{category}: Total {total_amount:.2f}")

    print("\nCategory Wise Spending:")
    for category, amount in self.categories.items():
        print(f"{category}: {amount:.2f}")

def main():
tracker = ExpenseTracker()

while True:
    print(" Your Expense Tracker Menu:")
    print("1. Add Expense")
    print("2. Show Summary")
    print("3. Quit")

    choice = input("Enter your choice: ")

    if choice == "1":
        amount = eval(input("Enter expense amount: "))
        category = input("Enter expense category: ")
        tracker.add_expense(amount, category)
        print("Expense added successfully!")
    elif choice == "2":
        tracker.show_summary()
    elif choice == "3":
        print("Exiting the Expense Tracker.")
        break
    else:
        print(" Please try again.")

if name == "main":
main()

#

basically its budget tracker that track your expenses

tiny crownBOT
#
Code Formatting

When sharing code with the community, please use the correct formatting for ease of readability.

Example

```py
YOUR CODE HERE
```

Those are back ticks not single quotes, typically the key above TAB

tiny crownBOT
# dense sapphire class ExpenseTracker: def __init__(self): self.expenses = {} ...

@olive shell

✅ Formatting - Success
class ExpenseTracker:
    def __init__(self):
        self.expenses = {}
        self.categories = {}

    def add_expense(self, amount, category):
        if category not in self.categories:
            self.categories[category] = 0
        self.categories[category] += amount

        if category not in self.expenses:
            self.expenses[category] = []
        self.expenses[category].append(amount)

    def show_summary(self):
        print("Expense Summary:")
        for category, amount_list in self.expenses.items():
            total_amount = sum(amount_list)
            print(f"{category}: Total {total_amount:.2f}")

        print("\nCategory Wise Spending:")
        for category, amount in self.categories.items():
            print(f"{category}: {amount:.2f}")


def main():
    tracker = ExpenseTracker()

    while True:
        print(" Your Expense Tracker Menu:")
        print("1. Add Expense")
        print("2. Show Summary")
        print("3. Quit")

        choice = input("Enter your choice: ")

        if choice == "1":
            amount = eval(input("Enter expense amount: "))
            category = input("Enter expense category: ")
            tracker.add_expense(amount, category)
            print("Expense added successfully!")
        elif choice == "2":
            tracker.show_summary()
        elif choice == "3":
            print("Exiting the Expense Tracker.")
            break
        else:
            print(" Please try again.")


if __name__ == "__main__":
    main()

olive shell
#
  • Don't use eval. Either use a type function (ex amount = int(input("Enter expense amount: "))) or use literal_eval since it won't run arbitrary code and will only parse literals (ints, floats, strings, lists, etc.).
  • Instead of adding a new category set to zero if the category is not found you could use a default dict. self.categories = collections.defaultdict(lambda: 0). That'll let you do self.categories[category] += amount and if the category isn't found it'll be created for you with the value 0. Same goes for self.expenses.
  • self.categories isn't actually categories, it's totals. So you should have self.expenses and self.totals. You don't even need to have a totals dictionary. You just need expenses since self.categories[category] is the same value as sum(self.expenses[category]), I'd prefer summing over precomputing unless there's a very good reason to be precomputing.
tiny crownBOT
# dense sapphire class ExpenseTracker: def __init__(self): self.expenses = {} ...

@dense sapphire

✅ Formatting - Success
class ExpenseTracker:
    def __init__(self):
        self.expenses = {}
        self.categories = {}

    def add_expense(self, amount, category):
        if category not in self.categories:
            self.categories[category] = 0
        self.categories[category] += amount

        if category not in self.expenses:
            self.expenses[category] = []
        self.expenses[category].append(amount)

    def show_summary(self):
        print("Expense Summary:")
        for category, amount_list in self.expenses.items():
            total_amount = sum(amount_list)
            print(f"{category}: Total {total_amount:.2f}")

        print("\nCategory Wise Spending:")
        for category, amount in self.categories.items():
            print(f"{category}: {amount:.2f}")


def main():
    tracker = ExpenseTracker()

    while True:
        print(" Your Expense Tracker Menu:")
        print("1. Add Expense")
        print("2. Show Summary")
        print("3. Quit")

        choice = input("Enter your choice: ")

        if choice == "1":
            amount = eval(input("Enter expense amount: "))
            category = input("Enter expense category: ")
            tracker.add_expense(amount, category)
            print("Expense added successfully!")
        elif choice == "2":
            tracker.show_summary()
        elif choice == "3":
            print("Exiting the Expense Tracker.")
            break
        else:
            print(" Please try again.")


if __name__ == "__main__":
    main()

dense sapphire
#

that help me lot thankx

humble jay
humble jay
#

can't they just remove these?

olive shell