Build a Budget App Project - Build a Budget App Project

It’s time for me to go to work! I will continue tomorrow. Thank you for your help so far! I will keep trying tomorrow!

if you want to ask help in debugging, you would need to share your code

Hello today again! This is my code it seems like i got stuck:


class Category:
    def __init__(self, category):
        self.category = category
        self.ledger = []
        self.budget = 0

    #the deposit method
    def deposit(self, amount, description=''):
        if description is None:
            return description
        else:
            self.ledger.append({'amount': amount, 'description': description})
        self.budget += amount

    #the withdraw method
    def withdraw(self, amount, description=''):
        if not self.check_funds(amount):
            return False
        else:
            self.budget -= amount
            self.ledger.append({'amount': -abs(amount), 'description': description})
            return True
        
    #the get balance method
    def get_balance(self):
        return self.budget

    #the transfer method
    def transfer(self, amount, other_budget):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {other_budget.category}')
            other_budget.deposit(amount, f"Transfer from {self.category}")
            return True
        return False
        
    #the check_funds methos
    def check_funds(self, amount):
        if amount > self.budget:
            return False
        return True
        
    def __str__(self):
        result = ''
        title = self.category.center(30, '*')
        result += f'{title}\n'
        for item in self.ledger:
            description = item["description"]
            amount = item["amount"]
            result += f"{description[:23]} {amount:7.2f}\n"
        result += f"Total: {self.budget: .2f}"
        return result

#create spend chart
def create_spend_chart(categories):
    amount_spent = []
    for cat in categories:
        spend = 0
        for item in cat.ledger:
            if item["amount"] < 0:
                spend += abs(item["amount"])
        amount_spent.append(round(spend, 2))
    
    #get the total amount spend
    total = round(sum(amount_spent, 2))
    #get the percentage for each category
    percentage_spent = [round(int((amount / total)*100))for amount in amount_spent]

    title = "Percentage spent by category\n"

    chart = ""   
    for i in range(100, -1, -10):
        chart += str(i).rjust(3) + '| '
        for percent in percentage_spent:
            if percent >= i:
                chart += 'o  '
            else:
                chart += "   "
        chart += "\n"

    
    footer = "    " + "-" * ((len(categories) * 3) + 1) + "\n"

    category_names = [category.category for category in categories]

    max_length = max(len(name) for name in category_names)
    all_names = [name.ljust(max_length) for name in category_names]

    for i in range(max_length):
        footer += "     "
        for name in all_names:
            footer += name[i] + "  "
        footer += "\n"
    
    return (title + chart + footer).rstrip('\n')


food = Category('Food')
food.deposit(1000,'A grant')
clothing = Category('clothing')
auto = Category('Auto')

food.transfer(560,clothing)
food.withdraw(345,'For  vegetables')
clothing.withdraw(440,'for  shoes)
clothing.transfer(40,auto)
food.transfer(70,auto)
auto.withdraw(90,'For  gaz')
print(auto)
# print(food)
groceries = [food, clothing, auto]
print(create_spend_chart(groceries))
# print(auto)

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

What are you stuck on?


I got it done thank you for your help!

I was stuck on the bar, for the ‘o’, but i realized that it should been my percentage calculation and i decided to work on it and then it works.
Thank you for your great Assistance.