Scientific Computing: Budget App - create_spend_chart

Hi everyone,

I’ve been stuck at the ‘spend chart’ part of this project for over two weeks now, with no end in sight. The output looks identical but i’m getting one failure which i know ,most likely, is due to newlines and spaces but i can’t figure it out even when i set self.maxDiff=None, as suggested.

The error is:

Diff is 819 characters long. Set self.maxDiff to None to see it. : Expected different chart representation. Check that all spacing is exact.

I’ve seen 2-3 similar posts here but for some reason there was no response, i hope this one will fare better .

Thanks in advance to anyone that will take the time to help
(link to project: https://replit.com/@EfstathiosGramm/boilerplate-budget-app-1#budget.py)

Your code so far

class Category:
    name = ""
    withdrawals = 0

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

    def __str__(self):
        represent = self.name.center(30, "*") + "\n"

        for item in self.ledger:
            row = f"{item['description'][:23]:23}{item['amount']:7.2f}"
            represent += f'{row}\n'

        represent += "Total: " + str(self.get_balance())
        return represent

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

    def check_funds(self, amount):
        total = 0
        for item in self.ledger:
            total += item['amount']
        if float(amount) <= total:
            return True
        else:
            return False

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

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

    def transfer(self, amount, category2):
        if self.check_funds(amount) is True:
            description1 = f'Transfer from {self.name}'
            description2 = f'Transfer to {category2.name}'
            d1 = {"amount": float(-amount), "description": description2}
            self.withdrawals += amount
            self.ledger.append(d1)
            d2 = {"amount": float(amount), "description": description1}
            category2.ledger.append(d2)
            return True
        else:
            return False


def create_spend_chart(categories):
    category = Category
    expenditure = dict()
    pct_exp = dict()
    total_exp = 0
    for category in categories:
        expenditure[category.name] = category.withdrawals
        total_exp += category.withdrawals
    for key, value in expenditure.items():
        pct_exp[key] = value/total_exp*100

    row = "Percentage spent by category"
    for i in range(100, -1, -10):
        row += f'\n{str(i).rjust(3)}|'
        for j in pct_exp.values():
            if j > i:
                row += " o "
            else:
                row += "   "
    row += "\n    ----------\n"

    max_length = max(expenditure.keys(), key=len)

    for i in range(len(max_length)):
        row += " " * 4
        for name in expenditure.keys():
            if i < len(name):
                row += " " + name[i] + " "
            else:
                row += " " * 3
        row += " \n"

    row = row.rstrip() + " " * 2

    return row

Your browser information:

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

Challenge: Budget App

Link to the challenge: https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/budget-app

hi, you can go to the tests file and check the supposed result of the test that fails. In this case, its a string, which your code is expected to reproduce. If you look closely you can notice how each new line is structured. For example at the end of every line, there is an extra white space ' ' . The separator line which you use (-------) is also supposed to be of adjustable length(“two spaces past the final bar”).

Here is link to my solution, which is processing the tests OK. Maybe looking at it you can figure out where your code differs in output

1 Like

First of all, thanks for taking the time to help.
I hadn’t realised the separator line was supposed to be adjustable, i’ll try amending my code with that and get back to you

Hi Sylvant - thanks for the tips. I just came on here short of pulling my hair out. It looked okay on the screen, but realised that the grader was very pedantic about spacing and \n characters. Combing through each error line as you suggested paid off. Thanks a mil!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.