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