Budget app formatting problem

Hello guys, when I try this code on PyCharm it works perfectly but on replit I encounter in this error, at this point I don’t know what can i modify, I’m stuck here from several time.
Can anyone help me? thanks!

Here’s my code:

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

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

    def deposit(self, amount, description=''):
        self.total = int(self.total + amount)
        self.description = description
        self.ledger.append({'amount': amount, 'description': description})

    def withdraw(self, amount, description=''):
        if self.total - amount >= 0:
            self.ledger.append({"amount": -1 * amount, "description": description})
            self.total -= amount
            return True
        else:
            return False

    def get_balance(self):
        return self.total

    def transfer(self, amount, categ):
        if self.withdraw(amount, "Transfer to {}".format(categ.name)):
            categ.deposit(amount, "Transfer from {}".format(self.name))
            return True
        else:
            return False

    def __repr__(self):
        FL = f"{self.name:*^30}\n"
        ledger = ""
        for item in self.ledger:
            # Description and amount
            des = "{:<23}".format(item["description"])
            amo = "{:>7.2f}".format(item["amount"])
            # Max len description 23 characters - Max len amount 7 characters
            ledger += "{}{}\n".format(des[:23], amo[:7])
        total = "Total: {:.2f}".format(self.total)
        return FL + ledger + total

food = Category('Food')
clothing = Category('Clothing')
entertainment = Category('Entertainment')

def create_spend_chart(categories):
    Descr = 'Percentage spent by category\n'

    tot = []  # Percentage for each cat
    names = []
    for category in categories:
        spent = 0
        for item in category.ledger:
            if item["amount"] < 0:
                spent += abs(item["amount"])
        tot.append(round(spent, 2))
        names.append(category.name)

    total = round(sum(tot), 2)
    NEWLIST = []
    for cat in tot:
        perc = int((cat / total) * 100)
        NEWLIST.append(perc)

    chart = ''
    for scal in range(100, -1, -10):
        chart = str(scal).rjust(3) + '|'
        for per in NEWLIST:
            if per >= scal:
                chart += " o "
            else:
                chart += "   "
        
        Descr += chart + '\n'

    dashes = '-' + '---' * len(categories)
    baseline = dashes.rjust((len(dashes) + 4)) + '\n'
    Descr += baseline

    dist = max(names, key=len)
    for x in range(len(dist)):
        nmst = '    '
        for name in names:
            if x >= len(name):
                nmst += '   '
            else:
                nmst += name[x] + '  '

        if(x != len(dist) -1):
            nmst += '\n'

        xax = ' '
        xax += nmst
        Descr += xax

    return Descr

This is the replit error:

FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-budget-app-6/test_module.py", line 102, in test_create_spend_chart
    self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
AssertionError: 'Perc[34 chars]     \n 90|         \n 80|         \n 70|    o[329 chars] t  ' != 'Perc[34 chars]      \n 90|          \n 80|          \n 70|  [340 chars] t  '

And this is my replit link:
Replit link

An example failing line is:

-   0| o  o  o $
+   0| o  o  o  $
?              +

I’ve added the $ at the end of the lines; yours is the - line and the expected is the + line and the ? line shows the difference. Your bar graph lines are missing something.

One thing to remember when testing at home with pycharm… to run all the tests, you need to execute main.py… not budget.py. Budget.py is just where you are writing your class and functions… but it doesn’t run any of the testing. Main.py runs the test suite which will run your code with the approrpiate tests… That is what the replit does and is probably why you get a different result in pycharm.

Thank you so much Jeremy, finally i found the fail problem!
Also thank you kinome for the suggestion.

I’m curious about this as I don’t use anything other than emacs (so I always explicitly run whatever, usually via make) and I always see this problem from others. So does pycharm only run the file that is being edited or is there some configuration to tell it to run another file (like the main.py or self-discover tests, etc.)? Could this configuration be added as a dotfile or similar that is pycharm specific so that pycharm automagically runs the main.py?

As you point out, there is repl.it configuration to run main.py already so it would be nice to duplicate this behavior for pycharm.

Yeah, I never used pycharm. There may be a way to implement that, but as it would only apply to the python projects for FCC, not sure why you would want to. Basically, when you execute a file, the file runs… if you need it to run something else, you would need to implement that in the original file.

The way these seem to be setup for the projects, is main.py imports your code, and the test code, and runs them together… your code just runs your code unless you tell it to do something different… its not really anything tricky or special. Replit does it because its a service, not a code editor, and you need to assign an action for the ‘play’ button.

But basically, if you want to compile your code to check for errors, execute your code file… if you want to run the FCC tests against your code, execute the main.py file.

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