Budget App chart test not passing and I think result is correct

Well, I’m a bit confused here bc I did the calculations manually from the steps on the test and it seems as my result is correct. But the test assertions give a different chart representation where Business Category is rounded down to 0 and Entertainment to 20 (when I get 10 for both). Does someone spot something off in my code? To round down the floats I used the int() function.

Thanks in advance!! And here’s my code:

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, "description": description})
            return True
            return False

    def get_balance(self):
        balance = 0
        for item in self.ledger:
            balance += item["amount"]
        return balance

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

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

    def __str__(self):
        title = f'{self.category:*^30}\n'
        body = ''
        total = 0
        for item in self.ledger:
            body += item["description"][:23].ljust(23) + str(
                '{:.2f}'.format(item["amount"]))[:7].rjust(7) + '\n'
            total += item["amount"]
        table = title + body + f'Total: {total:.2f}'
        return table

def create_spend_chart(categories):
    total_spent = 0
    longest = ''  # Queria saber longitud de la palabra mas larga para luego iterar por renglon
    for category in categories:
        category.spent = 0
        for item in category.ledger:
            if item["amount"] > 0:
                total_spent += item["amount"]
                category.spent += item["amount"]
    for category in categories:
        category.percentage = int(category.spent / total_spent * 100)
        if len(category.category) > len(longest):
            longest = category.category
    chart = 'Percentage spent by category\n'
    for i in range(100, -1, -10):
        chart += f'{i:>3}| '
        for category in categories:
            if i <= category.percentage:
                chart += 'o  '
                chart += '   '
        chart += '\n'
    width = len(categories) * 3 + 1
    chart += '    ' + '-' * width + '\n'
    for i in range(len(longest)):
        chart += '     '
        for category in categories:
                chart += category.category[i] + '  '
                chart += '   '
        chart += '\n'
    return chart

Take a look at the example chart printed after running main.py. Both food and auto have the same percent on plot, while for food was spent 76.05 and for auto 15. Maybe function is calculating these values for plot incorrectly?

1 Like

I came here to ask about this same issue (and have also posted about it here). From what I can tell the hard-coded “expected” string is incorrect, so I’m just going to add a note to my function’s docstring about it and submit my solution. There really ought to be some way to report issues like this to site admins, perhaps someone will see this thread but we can’t be the first people to encounter this issue by a long shot.

Edit: Did you also have an issue with the food category? If you check the math in the code for the unit test (initial depost of 900, withdrawal of 105.55, so percentage spent should be 11.72% or ~10% on the chart) it doesn’t match up to the chart, which shows a “bar” up to 70%.

1 Like

Take a one more look at the function description in README.md file, your function does something slightly different than what is expected.

1 Like

Yup, totally this code was wrong, i was using deposits instead of only withdrawals if i remember correctly. Later i did it right.

The only part of the solution that kept me confused for a while since i was getting the correct representation, was that i was either adding a new line at the end of the chart that wasn’t expected or i wasn’t adding it and it was expected. But i think the tests work fine.

Peace :call_me_hand:t4: