Tell us what’s happening:
I believe the project is totally complete, but it still tells me the chart is 1066 characters different from the original. I can’t seem to find the problem though
Edit: I think it’s mostly about blankspaces. I’m in doubt if they would refuse a perfectly working project if the output didn’t match 100% with the original
Thanks in advance!
Error message:
Traceback (most recent call last):
File “/home/runner/Budget-App/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[25 chars]n100|\n 90|\n 80|\n 70| o \n 60| o \n 50| o \n[264 chars]t \n’ != 'Perc[25 chars]n100| \n 90| \n 80| [349 chars] t ’
Diff is 1066 characters long. Set self.maxDiff to None to see it. : Expected different chart representation. Check that all spacing is exact.
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def __str__(self):
title = self.name.center(30, '*') + '\n'
middle = ''
for i in self.ledger:
middle += f'{i["description"]:23.23}' + f'{i["amount"]:>7.2f}' + '\n'
total = f'Total: {self.get_balance()}'
return title + middle + total
def deposit(self, amount, description=''):
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description=''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
return True
return False
def get_balance(self):
total = 0
for i in self.ledger:
total += i['amount']
return total
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {category.name}')
category.deposit(amount, f'Transfer from {self.name}')
return True
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
return True
def create_spend_chart(categories):
from math import floor
total_categories, lines = {}, {}
total_chart = 0
for category in categories:
for i in category.ledger:
if category.name not in total_categories:
total_categories[category.name] = 0
if i['amount'] < 0:
total_chart += i['amount']
total_categories[category.name] += i['amount']
for category in categories:
total_categories[category.name] /= total_chart
total_categories[category.name] = round(total_categories[category.name], 2)
for i in range(-1, floor(total_categories[category.name] * 10)):
if category.name not in lines:
lines[category.name] = ''
lines[category.name] += 'o'
output = 'Percentage spent by category\n'
for i in range(10, -1, -1):
bar = ''
for key, value in lines.items():
try:
bar += value[i].center(3)
except IndexError:
continue
output += f'{i * 10:>3}|{bar}\n'
output += f'{"":4}' + ''.rjust(len(categories) * 3 + 1, '-') + '\n'
categories_names = ''
categories_str = []
for i in range(0, len(categories)):
categories_str.insert(i, str(categories[i].name))
for i in range(0, len(max(categories_str, key=len))):
categories_names += f'{"":^4}'
for j in range(0, len(categories)):
category_name = categories_str[j]
try:
categories_names += f'{category_name[i]:^3}'
except IndexError:
categories_names += f'{"":^3}'
continue
categories_names += '\n'
output += categories_names
return output
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 Edg/90.0.818.62
.
Challenge: Budget App
Link to the challenge: