Build a Budget App Project - Build a Budget App Project

I pass all tests except the transfer method test, the test that should result in 854.33 and the create_spend_chart test. I have checked that the spacing is correct and don’t believe I have missed anything, and the results fort he other tests are what you would expect to see so I am stumped.
Any help would be appreciated.
P.S I am aware this is far from the most efficient way to do this.

Your code so far

class Category:
    def __init__(self, category_name):
        self.name = category_name
        self.ledger = []
 
    def __str__(self):
        self.output_grid = '*'*round(((30-len(self.name))/2)) + self.name + '*'*round(((30-len(self.name))/2))

        for transaction in self.ledger:
            self.output_grid += "\n"
            self.output_grid += transaction['description'][:min(23, len(transaction['description'])):] 
            self.output_grid += " " * (30-(len(str(format(transaction['amount'],".2f"))) + min(len(transaction['description']), 23))) + str(format(transaction['amount'], ".2f")[:min(7, len(str(format(transaction['amount'],".2f")))):])
        
        self.output_grid += '\n' + f"Total: {self.get_balance()}"

        return self.output_grid


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

    def withdraw(self, amount, description = ''):
        if self.check_funds(amount) == False:
            return False
        else:
            self.ledger.append({'amount': float(format(0 - amount, ".2f")), 'description': description})
            return True
    
    def get_balance(self):
        self.total = 0
        for transaction in self.ledger:
            self.total += float(transaction ['amount'])
        return format(self.total, ".2f")

    def transfer(self, amount, other_category):
        if self.check_funds(amount) == False:
            return False
        else:
            self.withdraw(amount,  f'Transfer to {other_category.name}')
            other_category.deposit(amount, f'Transfer from {self.name}')
            return True

    def check_funds(self, amount):
        if float(self.get_balance()) < float(amount):
            return False
        else:
            return True

def create_spend_chart(categories):
    spent = {}
    total = 0

    for category in categories:
        spent[category.name] = 0
        for i in range(0, len(category.ledger)):
            if category.ledger[i]['amount'] < 0:
                spent[category.name] += round(category.ledger[i]['amount'], 2)
        spent[category.name] = 0 - round(spent[category.name],2)
        total += spent[category.name]

    percentage = []
    for sp in spent:
        percentage.append(round((spent[sp] / total)*100,-1))

    final_chart = 'Percentage spent by category\n'
    
    for i in range(10,-1,-1):
        if i == 10:
            temp = str(i*10) + '|'
        elif i < 10 and i > 0:
            temp = " " + str(i*10) + '|'
        else:
            temp = "  " + str(i*10) + '|'
        
        for perc in percentage:
            if perc >= i*10:
                temp += ' o '
            else:
                temp += '   '
        temp += ' \n'
        final_chart += temp

    final_chart += "    " + "-"*(3*len(percentage)) + "-\n"

    length = 0
    for category in categories:
        if len(category.name) > length:
            length = len(category.name)

    for i in range(0,length):
        final_chart += "     "
        for category in categories:
            if len(category.name) > i:
                final_chart += category.name[i] + '  '
            else:
                final_chart += '   '
        final_chart += "\n"

    return final_chart


food = Category("Food")
food.deposit(900, 'deposit') 
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')
print(food.get_balance())

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

The get_balance method should return number, not a string.

For the chart, generally, if you open browser’s console, there will be details of the failing test, with specific information which lines are mismatched.

1 Like

The fix on the balance method fixed the transfer tests as well.
Cheers.

This is what I see in the console, I apologise if I am being a bit daft but I don’t understand what is different.

image

This can be a bit convoluted on the first look:

  • Line with - indicates line in the result from the function
  • Line with + indicates line from the expected result
  • Line with ? indicates specific place where is the difference

Looking at it there appear two problems:

  • One bar in chart is going too high.
  • There’s additional new line character at the end, which is not expected.
1 Like

I have been looking over my code and I cannot see how my graph is producing a bar that is too high since it says the other bars are 70 and 20 so it only makes sense that my bar goes up to 10, and if I was supposed to do this in a different way such that there was an error, I don’t know what method it wanted me to use so I am really unsure where to go from here.

In regards to the end of line problem however, I haven’t managed to sort it so far but I will keep on searching.

Take another look at instructions, as to how the the rounding of the values for the bars should be done.

1 Like

Well, I sincerely apologise for not being able to read simple instructions.

I appreciate all the help you have given me. :+1:

No need to apologize, :slight_smile: I have no doubts regarding your ability to read, as it can be observed in this thread.

It’s normal to be fooled by brain with incorrect assumptions, when there’s many details to follow.

1 Like

Thank you so much and as soon as I fixed the rounding error, I somehow just knew what to change to fix my extra character error as well.

Cheers again.

1 Like