Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I don’t understand why my code doesn’t meet this requirement:

The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.

Your code so far

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

    def deposit(self, amount, description = ""):
        self.ledger.append({'amount': amount, 'description': description})

    def withdraw(self, amount, description = ""):
        if self.check_funds(amount):
            self.ledger.append({'amount': amount*(-1), 'description': description})
            return True
        else: 
            return False

    def get_balance(self):
        return sum([self.ledger[i].get("amount") for i in range(len(self.ledger))]) 
    
    
    def transfer(self, amount, destination_category):
        if not self.check_funds(amount):
            return False
        else:
            description = f"Transfer to {destination_category.category}"
            self.withdraw(amount,description)
            destination_description = f"Transfer from {self.category}"
            destination_category.deposit(amount,destination_description)
            return True

    def check_funds(self,amount):
        return self.get_balance() >= amount

    def __str__(self):
        string = ""
        first_line = ((30-len(self.category))//2)*"*" + self.category + round(((30-len(self.category))//2))*"*"
        string += first_line
        total = 0
        for d in self.ledger:
            description_string = f"\n{d['description'] if len(d['description']) < 24 else d['description'][:23]}"
            amount = f"{round(d['amount'],2):.2f}"
            if len(amount) > 7:
                amount = amount[:7]
            amount_string = f"{' ' * (31 - len(description_string) - len(str(amount)))}{amount}"
            string += f"{description_string}{amount_string}"
            total += d['amount']
        string += f"\nTotal: {round(total,2):.2f}"
        return string


def create_spend_chart(categories):
    chart_string = "Percentage spent by category"
    category_spendings = []
    category_percentages = []
    for category in categories:
        category_spendings.append(sum([category.ledger[i].get("amount") if category.ledger[i].get("amount") < 0 else 0 for i in range(len(category.ledger))]) )
    total_spendings = sum(category_spendings)
    category_percentages = [(category_spendings[i] / total_spendings)*100 for i in range(len(category_spendings))]
    for i in range(11):
        # percentage number
        percentage = 100-i*10
        number_string = str(percentage)
        bars = ""
        if len(str(percentage)) == 2:
            number_string = f" {percentage}"
        elif len(str(percentage)) == 1:
            number_string = f"  {percentage}"
        for p in category_percentages:
            if p >= percentage:
                bars += " o "
        if len(bars) < len(categories) * 3:
            bars += " "*(len(categories)*3-len(bars))
        chart_string += f"\n{number_string}|{bars} "
    chart_string += f"\n    {'-'*(3*len(categories)+1)}"
    names = [c.category for c in categories]
    for i in range(len(max(names, key = len))):
        chart_string += "\n    "
        names_string =""
        for n in names:
            if len(n)-1 >= i:
                names_string += f" {n[i]} "
            else:
                names_string += "   "
        if len(names_string) < len(categories) * 3:
            names_string += " "*(len(categories)*3-len(names_string))
        chart_string += f"{names_string} "
    return chart_string

food = Category("food")
not_food = Category("nots")
idk = Category("idkdda")
idk.deposit(232323)
idk.withdraw(232)
food.deposit(20203,"burger")
food.transfer(10033,not_food)
not_food.withdraw(2333,"sdf")
print(create_spend_chart([food,not_food,idk]))

Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Can you explain how your logic is rounding down?

opening the browser console for more details

AssertionError: 'Perc[74 chars] 70| o        \n 60| o        \n 50| o        [300 chars] t  ' 
             != 'Perc[74 chars] 70|    o     \n 60|    o     \n 50|    o     [300 chars] t  '

there is something wrong with your bars

Checking it out, your calculations do not work that well

It should be 20% for food, 30% for not_food and 50% for idk