Tell us what’s happening:
All tests for the chart fail but when I call the method it seems to work just fine. I have no clue what is going on
Your code so far
class Category:
def __init__ (self, name):
self.name = name
self.ledger = []
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
else:
return False
def get_balance(self):
balance = 0
for transaction in self.ledger:
balance += transaction['amount']
return balance
def check_funds(self, amount):
if amount <= self.get_balance():
return True
else:
return False
def transfer (self, amount, category_instance):
if self.check_funds(amount):
self.withdraw(amount,f'Transfer to {category_instance.name}')
category_instance.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def __str__(self):
lines = [set_title(self.name)]
for transaction in self.ledger:
description = transaction['description'][:23]
amount = '{:.2f}'.format(transaction['amount'])
lines.append(f'{description.ljust(23)}{amount.rjust(7)}')
lines.append(f'Total: {str(self.get_balance())[:8]}')
return '\n'.join(lines)
def set_title(name):
NLen = len(name)
LPad = (30-NLen)//2
RPad = round((30-NLen)/2)
return '*'*LPad + name + '*'*RPad
def create_spend_chart(categories):
chart_str = 'Percentage spent by category\n'
withdraw_list = []
separate_amount = []
# Get the total amount spent
for category in categories:
for transaction in category.ledger:
if transaction['amount']<0:
withdraw_list.append(-transaction['amount'])
separate_amount.append(round(sum(withdraw_list),2))
total_spent = sum(withdraw_list)
# Get the amount spent per category
index = 0
for value in separate_amount:
if index == 0:
pass
elif index == 1:
amnt = separate_amount[1] - separate_amount[0]
separate_amount[1]=amnt
elif index == 2:
amnt = separate_amount[2] -separate_amount[1] -separate_amount[0]
separate_amount[2] = amnt
count = 0
extra_amnt = 0
index +=1
# Get the percentages
cat_1 = round((separate_amount[0]*100)//total_spent,2)
cat_2 = round((separate_amount[1]*100)//total_spent,2)
cat_3 = round((separate_amount[2]*100)//total_spent,2)
percent_list = [cat_1,cat_2,cat_3]
percentages = {}
ix_2 = 0
for name in percent_list:
percentages.update({categories[ix_2].name : percent_list[ix_2]})
ix_2 +=1
# create the chart
for x in range(100,-1,-10):
line = f'{x:3}| '
for value in percentages.values():
if value >= x:
line += 'o '
chart_str += line + '\n'
chart_str += ' '+ '-'*10
# Print names Vertically
longest_name = max(len(name) for name in percentages.keys())
for i in range(0,longest_name):
lines = ' '
for name in percentages:
if i < len(name):
lines += name[i] + ' '
else:
lines += ' '
chart_str += '\n' + lines
print(chart_str)
food= Category('Food')
clothing = Category('Clothing')
auto = Category('Auto')
food.deposit(1000, 'Initial Deposit')
food.withdraw(39.54, 'Restaurant')
food.withdraw(10.54, 'groceries')
food.transfer(200, auto)
clothing.deposit(600, 'Initial depo')
clothing.withdraw(25.76, 'Zara')
clothing.withdraw(67.39, 'shoes')
auto.deposit(500, 'Initial Depo')
auto.withdraw(300, 'mechanic')
auto.withdraw(56.38, 'gas')
create_spend_chart([food,clothing,auto])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App

