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