Tell us what’s happening:
I can’t pass the last test. I tried with several options, and even replaced all the white space with tildes to make sure my spacing was correct. I’ve removed the final white line, so that isn’t it. I don’t understand the debugger, I don’t see how I can replicate the mistake it’s apparently making. Please help!
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 transfer(self,amount,destination):
if not self.check_funds(amount):
return False
else:
self.withdraw(amount, f"Transfer to {destination.name}")
destination.deposit(amount, f"Transfer from {self.name}")
return True
def check_funds(self,amount):
if self.get_balance() >= amount:
return True
else:
return False
def truncate(self, string, length):
return str(string)[:length]
def __str__(self):
result = ""
total = self.get_balance()
title = self.name.center(30, "*")
result += title + '\n'
for entry in self.ledger:
right = f"{entry['amount']:.2f}"
left = self.truncate(entry['description'],23)
right = self.truncate(right,7)
result += left+" "*(30-len(left)-len(right))+ right + "\n"
result += 'Total: '+ f'{total:.2f}'
return result
#This is where the fun begins...
def create_spend_chart(categories):
cat_withdrawal = {}
perc_withdrawal = {}
chartstr = ""
total = 0
for i in categories:
withdrawal = 0
for x in i.ledger:
if x['amount'] < 0:
withdrawal += abs(x['amount'])
total += withdrawal
cat_withdrawal[i.name] = withdrawal
for index in cat_withdrawal:
perc_withdrawal[index] = int(f"{cat_withdrawal[index]/total*10:.0f}")*10
chartstr += "Percentage spent by category\n"
for p in range(100,-10,-10):
if p == 100:
chartstr += str(p) + "| "
elif p == 0:
chartstr += " " + str(p) + "| "
else:
chartstr += " " + str(p) + "| "
for index in cat_withdrawal:
if p <= cat_withdrawal[index]:
chartstr+= "o "
else:
chartstr += " "
chartstr += "\n"
count = 0
for i in categories:
count += 1
chartstr += " "+"-"*(count*3+1)+ "\n"
list = [i.name for i in categories]
catlength = 0
for i in list:
if len(i) > catlength:
catlength = len(i)
x=0
for i in range(catlength):
chartstr += " "
for item in list:
if x < len(item):
chartstr += item[x]+' '
else:
chartstr +=' '
x += 1
chartstr += '\n'
chartstr = chartstr[0:-1]
return chartstr
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0
Challenge Information:
Build a Budget App - Build a Budget App