Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I cant seem to figure out what the problem is, i get the correct output on my computer but I keep failing tests 11, 16 and 17.

Your code so far

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

    def __str__(self):
        output = f"{self.name.center(30, '*')}\n"
        for item in self.ledger:
            description = item['description']
            amount = item['amount']
            output += f"{description[:23]:23}{amount:>7.2f}\n"
        output += f"Total: {self.get_balance():>7.2f}"
        return output

    def deposit(self, amount, description=""):
        if not isinstance(amount, (int, float)):
            raise ValueError("Deposit must be a positive number")
        self.ledger.append({'amount':amount, 'description':description})
        
    def get_balance(self):
        balance = 0
        for item in self.ledger:
            balance += item["amount"]
        return balance
    
    def check_funds(self, amount):
        if not isinstance(amount, (int, float)) or amount <= 0:
            raise ValueError("Check funds amount must be a positive number.")
        return self.get_balance() >= amount
    
    def withdraw(self, amount, description=''):
         if not isinstance(amount, (int, float)):
            raise ValueError("Withdrawal must be a positive number")
         if self.check_funds(amount):
             self.ledger.append({'amount':-amount, 'description':description})
             return True
         return False


    def transfer(self, amount, category):
        if not isinstance(amount, (int, float)) or amount <= 0:
            raise ValueError("Transfer must be a positive number")
        if self.check_funds(amount):
            self.withdraw(amount, "Transfer to " + category.name)
            category.deposit(amount, "Transfer from" + self.name)
            return True  
        return False

def create_spend_chart(categories):
    total_withdrawals = 0
    category_withdrawals = {}

    for category in categories:
        category_withdrawals[category.name] = 0 
        for item in category.ledger:
            if item["amount"] < 0:  
                category_withdrawals[category.name] += abs(item["amount"]) 
                total_withdrawals += abs(item["amount"]) 
    if total_withdrawals == 0:
      return "Percentage spent by category\nNo withdrawals to display"

    chart = "Percentage spent by category\n"
    percentages = {}
    for category_name, withdrawals in category_withdrawals.items(): 
        percentages[category_name] = int((withdrawals / total_withdrawals) * 100) 
    for i in range(100, -1, -10):
        chart += f"{i:3}| "
        for category_name in category_withdrawals:
            if percentages[category_name] >= i: 
                chart += "o  " 
            else:
                chart += "   " 
        chart += "\n" 

    chart += "    " + ("---" * len(categories)) + "-\n" 
    max_name_length = max(len(category.name) for category in categories) 

    for i in range(max_name_length): 
      chart += "     " 
      for category in categories: 
        if i < len(category.name): 
          chart += category.name[i] + "  " 
        else:
          chart += "   " 
      chart += "\n" 

    return chart

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

If you take a look at the browser’s console, there will be details regarding the failing tests.

I copied your code and these are the details from the console,

// running tests 11. The

transfer

method should create a specific ledger item in the category object passed as its argument. 16. Printing a

Category

instance should give a different string representation of the object. 17.

create_spend_chart

should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details. // tests completed

That’s console on page. There’s more details in browser’s console.

Oh good to know, I figured they were giving the same output.