Build a Budget App - Build a Budget App

Tell us what’s happening:

I’m able to get expected output when I try it on idle, but it doesn’t pass the test cases from 16th one. Need help in finding the error, if I’m overlooking.
Thanks

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.total = 0
        self.balance = 0 
        self.category_wise_spent = 0
        self.ledger = []

    def deposit(self, amount, description = ""):
        self.balance+= amount
        self.ledger.append({'amount':amount, 'description' : description})
    
    def withdraw(self, amount, description = ""):
        if self.check_funds(amount):
            self.ledger.append({'amount': -amount, 'description': description})
            self.balance-= amount
            self.category_wise_spent += amount
            return True
        else:
            return False
    
    def get_balance(self):
        return self.balance

    def get_spent_total(self):
        return self.category_wise_spent

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

    def __str__(self):
        """title = '*'*30
        half = len(self.name)//2
        title = title[:15-half] + self.name + title[15+len(self.name)-half:]
        result = [title]"""
        title_length = len(self.name)
        left_padding = (30 - title_length) // 2
        right_padding = 30 - title_length - left_padding
        result = ['*' * left_padding + self.name + '*' * right_padding]
        if self.ledger:
            for txn in self.ledger:
                body=""
                body+= txn['description']
                body+= " "*23
                body = body[:23]
                value= " "*5 + f"{txn['amount']:= .2f}"
                body+= value[-7:]
                result.append(body) 
            result.append(f"Total: {self.get_balance():= .2f}")
        return "\n".join(result)
            
    
    def transfer(self, amount, destination_category):
        if self.check_funds(amount):
            self.withdraw(amount,f"Transfer to {destination_category.name}")
            destination_category.deposit(amount,f"Transfer from {self.name}")
            return True
        else:
            return False


def create_spend_chart(categories):
    title = 'Percentage spent by category'
    total_spent = 0
    category_spent = {}
    category_percent = {}
    final_chart = {}
    for category in categories:
        total_spent += category.get_spent_total()
        category_spent[category.name] = category.get_spent_total()
    for category in category_spent:
        category_percent[category] = round(category_spent[category]*10/total_spent)
    y_axis = { str(i)+'|': " " for i in range(100,-10,-10) }
    y_labels = []
    charts = []  
    x_axis= []
    x_labels =[]
    for category in categories:
        x_axis.append(category.name)
    for category in category_percent:
        for i in range(category_percent[category]+1):
            y_axis[str(i*10)+'|']+= 'o  '
    for i in y_axis:
        y_=""
        if len(i) != 4:
            y_ = " "*(4-len(i))
            y_+=i
            y_+=y_axis[i]
        else:
            y_ = i+y_axis[i]
        y_labels.append(y_)
    h_padding = "    "
    h_line= h_padding + '-'* (len(x_axis)*3 +1)
    y_labels.append(h_line)
    max_length = max(len(name) for name in x_axis)

    for i in range(max_length):
        x_ = "     "
        for name in x_axis:
            if i < len(name):
                x_+= name[i] + "  "
            else:
                x_+= "   "
        x_labels.append(x_)
    charts = y_labels
    charts.extend(x_labels)
    print(charts)

    
    return "\n".join(charts)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:146.0) Gecko/20100101 Firefox/146.0

Challenge Information:

Build a Budget App - Build a Budget App

open the browser console, there is extra output there when you run the tests, for example:

AssertionError: '****[76 chars]gs, bac -45.67\nTransfer to Entertainme -20.00\nTotal:  834.33' 
             != '****[76 chars]gs, bac -45.67\nTransfer to Entertainme -20.00\nTotal: 834.33'

the first is your code, the second is the expected, notice where there is a difference

I have corrected 16th test case, but now I couldn’t figure it out why 19,20 and 24 are failing. I presume create_spend_chart is giving expected output string when I check with print statement.

if you open the browser console the tests output there similar messages for all the failed tests, you should be able to find them yourself

otherwise, I will need to be at my pc to do the same

Please share your updated code

Thanks everyone, I was able to pass the all the test cases.
Thanks again.

1 Like