Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

i been stuck on the Budget App Project on spend_chart, i looked at the forums for help, i tryed f12 console and im still stuck, the chart in my opinion looks 1 to 1 and i dont see any problems whit it is it a logic error on how i caculate the percent?

Your code so far


import math

class Category:
    
    def __init__(self, category):
        self.category = category 
        self.budget = 0.0 
        self.ledger = [] 
    
    #Done
    def deposit(self, amount, description = ""): 
        self.budget += amount 
        self.ledger.append({"amount": amount, "description": description}) 
        return True 
    
    #Done
    def withdraw(self, amount, description = ""):
        if self.check_funds(amount): 
            self.budget -= amount
            self.ledger.append({"amount": -(amount), "description": description})
            return True 
        else:
            return False 
    
    #Done
    def get_balance(self):
        return self.budget
    
    #Done
    def check_funds(self, amount):
        if self.budget >= amount:
            return True
        else:
            return False
    
    #Done
    def transfer(self, amount, new_category): 
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {new_category.category}")
            new_category.deposit(amount, f"Transfer from {self.category}")
            return True
        else:
            return False

    #Done
    def __str__(self):
        table = "" 
        title = self.category.center(30, "*")
        table += title + "\n" 

        for item in self.ledger:
            description = item["description"] 
            amount = item["amount"]
            description = description[:23]
            formated_amount = f"{amount:7.2f}"
            table += f"{description:<23}{formated_amount}" + "\n"
        
        table += "Total: "
        table += format(self.budget, ".2f")

        return table
    
food = Category("Food")
clothing = Category("Clothing")
auto = Category('Auto')

food.deposit(1000, "deposit")
food.withdraw(600, "groceries")
food.withdraw(165.60, "books")
food.withdraw(175.10, "books")

clothing.deposit(1000, "deposit")
clothing.withdraw(200, "groceries")

auto.deposit(1000, "deposit")
auto.withdraw(100, "groceries")

print(food)
print(clothing)
print(auto)

def create_spend_chart(categories):
    table = "Percentage spent by category  \n"
    total_spent = 0
    spent_amount = {}
    cat_name = []
    cat_percent = []
    
    for item in categories:
        cat_name.append(item.category)
        spent_amount = sum(abs(val["amount"]) for val in item.ledger if val["amount"] < 0)
        total_spent = sum(val["amount"] for val in item.ledger if val["amount"] > 0)
        spent_amount = (spent_amount / total_spent) * 100
        cat_percent.append(int(spent_amount // 10) * 10)
    
    for num in range(100, -10, -10):
        table += str(num).rjust(3) + "| "
        for spent_amount in cat_percent:
            if num <= spent_amount:
                table += "o  "
            else:
                table += "   "
        table += "\n"

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

    max_len = max(len(category) for category in cat_name) 
    for i in range(max_len):
        word_vertical = "     "
        for category in cat_name:
            if i < len(category):
                word_vertical += category[i] + "  "
            else:
                word_vertical += "   "
        word_vertical += "\n"
        table += word_vertical
    print(table)

create_spend_chart([food, clothing, auto])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

First of all, you need to return (not print) the chart.

Then if you open the console (F12) and run the tests you’ll see the difference respect to the expected output.