Python Projects:Budget App:Problems with the last test

Tell us what’s happening:
I’ve checked my create_spend_chart function many times, but still, I don’t know why my code fails the last test case. I compared my output with the last test case output and found that categories are only placed differently. What’s the problem with create_spend_chart function? All other functions seem to be alright and pass all the tests. Any help would be greatly appreciated.

Your code so far

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

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

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

    def get_balance(self):  
        return sum([item["amount"] for item in self.ledger])

    def transfer(self, amount_to_transfer, where_to_transfer):
        if self.check_funds(amount_to_transfer) is False:
            return False
        self.withdraw(amount_to_transfer, f"Transfer to {}")
        where_to_transfer.deposit(amount_to_transfer, f"Transfer from {}")
        return True

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

    def __str__(self):
        title = ""
        for x in range(15 - (len(
            title += "*"
        title +=    
        for x in range(30 - len(title)):
            title += "*"           
        content = ""
        for item in self.ledger:
            content += item["description"][:23:]
            for x in range(24 - len(item["description"][:23:])):
                content += " "
            content += "{:.2f}".format(item["amount"])
            content += "\n"
        content += "Total: {:.2f}".format(self.get_balance())    
        return title + "\n" + content       

def create_spend_chart(instances):
    output = "Percentage spent by category\n"       
    percentage = 100
    sum_of_withdraws = 0
    for instance in instances:
        sum_of_withdraws += -sum([x["amount"] for x in instance.ledger if x["amount"] 
< 0])
    while percentage >= 0:
        if len(str(percentage)) == 3:
            output += str(percentage) + "|"
        elif len(str(percentage)) == 2:
            output += " " + str(percentage) + "|"
            output += "  " + str(percentage) + "|"
        for instance in instances:
            spent = (-sum([x["amount"] for x in instance.ledger if x["amount"] < 
            if spent < 10:
                spent = 0
                if int(str(spent)[1]) >= 5:
                    change_spent = str(spent).split(".")
                    value = int(change_spent[0])
                    while value % 10 != 0:
                        value += 1
                    spent = value
            if spent >= percentage:
                output += " o "
                output += "   "
        output += " \n"        
        percentage -= 10
    output += "    -"
    for x in range(len(Category.instances)*3):
        output += "-"   
    output += "\n"     

    longest_instance_name = 0
    for instance in instances:
        if len( > longest_instance_name:
            longest_instance_name = len(

    updated_names = [] 
    for instance in instances:
        while len( < longest_instance_name:
    += " "
    if len(updated_names) == 1: 
        for letter in updated_names[0]:
            output += f"     {letter}  \n"
    elif len(updated_names) == 2:
        for letter in range(len(updated_names[0])):
            output += f"     {updated_names[0][letter]}  {updated_names[1][letter]}  \n"  
    elif len(updated_names) == 3:
        for letter in range(len(updated_names[0])):
            output += f"     {updated_names[0][letter]}  {updated_names[1][letter]}  
{updated_names[2][letter]}  \n"    
    elif len(updated_names) == 4:    
        for letter in range(len(updated_names[0])):
            output += f"     {updated_names[0][letter]}  {updated_names[1][letter]}  
{updated_names[2][letter]}  {updated_names[3][letter]}  \n"
    return output.rstrip() + "     "

Your browser information:

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

Challenge: Budget App

Link to the challenge:

It’s a big ask to have someone go through 100+ lines of code to figure out where an error might be. I think a good place to start would be to add comments to your code and in the process you may discover what your want the code to do and what it actually does are two separate things.

Did you fix already your last function?
If not (or at next time), one of the easiest way to find the different between the test result and your solution if you take a look to the test file.
The last test you find in from line 85.
In your comment out everything except import statement and copy

this statements
food = budget.Category("Food")
entertainment = budget.Category("Entertainment")
business = budget.Category("Business")

food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")

actual = create_spend_chart([business, food, entertainment])
expected = "Percentage spent by category\n100|          \n 90|          \n 80|          \n 70|    o     \n 60|    o     \n 50|    o     \n 40|    o     \n 30|    o     \n 20|    o  o  \n 10|    o  o  \n  0| o  o  o  \n    ----------\n     B  F  E  \n     u  o  n  \n     s  o  t  \n     i  d  e  \n     n     r  \n     e     t  \n     s     a  \n     s     i  \n           n  \n           m  \n           e  \n           n  \n           t  "

print("Actual: " + str(len(actual)))
print("Expected: " + str(len(expected)))

Run your code and try to find the different. Maybe just some space need to add more or delete…

1 Like

I’ve solved it. The problem was that I counted all instances of Category class. I should only have counted the number of instances of input.