Tell us what’s happening:
My code seems to be fine, the output is the exact same but it fails the last test. I found a post in this forum suggesting locating assertion errors but I couldn’t figure out how to test it on my own.
Your code so far
from decimal import *
getcontext().prec = 4
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.expenses = []
def __str__(self):
title = f'{self.name:*^30}\n'
items = ''
total = f'Total: {self.get_balance()}'
for item in self.ledger:
items += f'{item["description"][0:23]:23}' + f'{item["amount"]:>7.2f}' + '\n'
output = title + items + str(total)
return output
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})
self.expenses.append(-amount)
return True
return False
def get_balance(self):
balance = 0
for item in self.ledger:
balance += item["amount"]
return balance
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 self.get_balance() >= amount:
return True
return False
def spent(self):
spent = 0
for value in self.expenses:
spent += Decimal(value)
return spent
def create_spend_chart(categories):
title = 'Percentage spent by category'
line = '\n'
total_expenses = 0
percentage = {}
per_str = {}
rounder_per = {}
dashes = ' '
names = []
for category in categories:
dashes += '---'
names.append(category.name)
total_expenses += category.spent()
percentage = {category.name: float((category.spent()/total_expenses)*100) for category in categories}
rounded_per = {category.name: int(str(percentage[f'{category.name}'])[0] + '0') for category in categories}
dashes += '-'
axis = ''
maior = max(names, key=len)
for i in range(len(maior)):
vertical_name = ' '
for name in names:
if i>=len(name):
vertical_name += ' '
else:
vertical_name += name[i] + ' '
if i!=(len(maior) - 1):
vertical_name += '\n'
axis += vertical_name
for num in range(100, -10, -10):
line += f'{str(num).rjust(3)}|'
for category in categories:
if rounded_per[f'{category.name}']>=num:
line += ' o '
else:
line += ' '
line += '\n'
output = title + line + dashes + '\n' + axis
return output
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Budget App