Need help with budget app chart

Hello guys.
I have some issue with chart. When I check it in test_module i got this error : “Traceback (most recent call last):
File “/home/runner/boilerplate-budget-app-14/test_module.py”, line 94, in test_create_spend_chart
self.assertEqual(actual, expected, ‘Expected different chart representation. Check that all spacing is exact.’)
AssertionError: 'Perc[170 chars] 10| o o o \n 0| o o o \n ----------[204 chars] t ’ != 'Perc[170 chars] 10| o o \n 0| o o o \n ----------[204 chars] t '”

But when I run it on PyCharm it looks good to me. That is only error that I’m getting and I tried to find mistake but unsuccessfully. I even replaced all spacings with “_” to check it and its all fine.
I’m gonna post my code but I don’t have many comments, let me know if you need any explanation. Don’t mind Class just function outside class.

Here is my code:

class Category:

    def __init__(self, category):
        self.category = category
        self.ledger = list()

    def deposit(self, deposit_amount, deposit_description=""):
        # deposit money to ledger ( append to list )
        self.depo = dict()
        self.deposit_amount = deposit_amount
        self.deposit_description = deposit_description
        self.depo["amount"] = self.deposit_amount
        self.depo["description"] = self.deposit_description
        self.ledger.append(self.depo)

    def withdraw(self, withdraw_amount, withdraw_description=""):
        # taking money from ledger - (import as negative numbers)
        self.withd = dict()
        self.withdraw_amount = withdraw_amount * -1
        self.withdraw_description = withdraw_description
        if self.check_funds(withdraw_amount) is True:
            try:
                if self.get_balance() > 0:
                    self.withdrawing = self.get_balance() + self.withdraw_amount
                    self.withd["amount"] = self.withdraw_amount
                    self.withd["description"] = self.withdraw_description
                    self.ledger.append(self.withd)
                    return True
            except:
                if self.get_balance() < 0:
                    return False
        else:
            return False

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

    def transfer(self, transfer_amount, destination):
        self.transfer_amount = transfer_amount  # food.transfer_amount = 50
        self.destination = destination  # food.transfer = clothing
        if self.check_funds(transfer_amount) is True:
            self.withdraw(transfer_amount, "Transfer to " + destination.category)
            destination.deposit(transfer_amount, "Transfer from " + self.category)
            return True
        else:
            return False

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

    def __str__(self):
        self.stars = "*"
        self.header_align = self.stars * int(((30 - len(self.__dict__["category"])) / 2))
        self.header = self.header_align + self.__dict__["category"] + self.header_align
        self.line = ""
        self.shift = ""
        for x in range(len(self.ledger)):
            self.line += "{:<23}".format(self.ledger[x]["description"][0:23]) + "{:>7.2f}".format(
                float(self.ledger[x]["amount"])) + "\n"
        return self.header + "\n" + self.line + "Total:" + " " + "{:.2f}".format(self.get_balance())


def create_spend_chart(category):

    iznosi = dict()
    for x in category:
        x.total = 0
        for y in range(len((x.ledger))):
            if x.ledger[y]["amount"] < 0:
                x.total += x.ledger[y]["amount"] * -1
                iznosi[x.__dict__["category"]] = x.total
    total = 0
    num = list()
    name = list()
    for key, value in iznosi.items():
        total += value
    for key, value in iznosi.items():
        iznosi[key] = value / total
    for key, value in iznosi.items():
        if int(str(value).split(".")[1][1]) < 5:
            num.append(int(str(value).split(".")[1][0]) * 10)
        else:
            num.append(int(str(value).split(".")[1][0]) * 10 + 10)
        name.append(key)

    result = "Percentage spent by category\n"
    count = 100
    circles = ""
    while count >= 0:
        for x in num:
            if count > x:
                circles += "   "
            if count <= x:
                circles += "o" + "  "
        if count == 0:
            result += str(count).rjust(3) + "|" + " " + circles + "\n"
        else:
            result += str(count).rjust(3) + "|" + " " + circles + "\n"
        circles = ""
        count -= 10
        if count < 0:
            break
    result += "    " + "-" * 10 + "\n"
    final_name = ""
    sorted_names = sorted(name, key=len)
    name_count = len(sorted_names[-1])
    name_count_start = 0

    while name_count_start <= name_count:
        for x in name:
            if len(x) <= name_count_start:
                final_name += "   "
            elif len(x) > name_count_start:
                final_name += x[name_count_start] + "  "
        if name_count_start == name_count-1:
            result += "     " + final_name
            break
        else:
            result += "     " + final_name + "\n"

        final_name = ""
        name_count_start += 1

    return result

Edit:
You can check code with:
food = Category(“Food”)
entertainment = Category(“Entertainment”)
business = Category(“Business”)
food.deposit(900, “deposit”)
entertainment.deposit(900, “deposit”)
business.deposit(900, “deposit”)
food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)
print(create_spend_chart([business, food, entertainment]))

Test output shows that height of bars on the chart is off. Take a look in the README.md file how these values should be rounded on chart.

1 Like

I didn’t understand what is required. So for example if number is 0.85 I should round it to 90 as in math, or to 80? I’m not native english speaker so that is kinda confusing to me

Just figured it out mate, thanks for help I passed the test! :smiley:

1 Like