Tell us what’s happening:
I’m able to get expected output when I try it on idle, but it doesn’t pass the test cases from 16th one. Need help in finding the error, if I’m overlooking.
Thanks
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.total = 0
self.balance = 0
self.category_wise_spent = 0
self.ledger = []
def deposit(self, amount, description = ""):
self.balance+= amount
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.balance-= amount
self.category_wise_spent += amount
return True
else:
return False
def get_balance(self):
return self.balance
def get_spent_total(self):
return self.category_wise_spent
def check_funds(self,amount):
return amount <= self.get_balance()
def __str__(self):
"""title = '*'*30
half = len(self.name)//2
title = title[:15-half] + self.name + title[15+len(self.name)-half:]
result = [title]"""
title_length = len(self.name)
left_padding = (30 - title_length) // 2
right_padding = 30 - title_length - left_padding
result = ['*' * left_padding + self.name + '*' * right_padding]
if self.ledger:
for txn in self.ledger:
body=""
body+= txn['description']
body+= " "*23
body = body[:23]
value= " "*5 + f"{txn['amount']:= .2f}"
body+= value[-7:]
result.append(body)
result.append(f"Total: {self.get_balance():= .2f}")
return "\n".join(result)
def transfer(self, amount, destination_category):
if self.check_funds(amount):
self.withdraw(amount,f"Transfer to {destination_category.name}")
destination_category.deposit(amount,f"Transfer from {self.name}")
return True
else:
return False
def create_spend_chart(categories):
title = 'Percentage spent by category'
total_spent = 0
category_spent = {}
category_percent = {}
final_chart = {}
for category in categories:
total_spent += category.get_spent_total()
category_spent[category.name] = category.get_spent_total()
for category in category_spent:
category_percent[category] = round(category_spent[category]*10/total_spent)
y_axis = { str(i)+'|': " " for i in range(100,-10,-10) }
y_labels = []
charts = []
x_axis= []
x_labels =[]
for category in categories:
x_axis.append(category.name)
for category in category_percent:
for i in range(category_percent[category]+1):
y_axis[str(i*10)+'|']+= 'o '
for i in y_axis:
y_=""
if len(i) != 4:
y_ = " "*(4-len(i))
y_+=i
y_+=y_axis[i]
else:
y_ = i+y_axis[i]
y_labels.append(y_)
h_padding = " "
h_line= h_padding + '-'* (len(x_axis)*3 +1)
y_labels.append(h_line)
max_length = max(len(name) for name in x_axis)
for i in range(max_length):
x_ = " "
for name in x_axis:
if i < len(name):
x_+= name[i] + " "
else:
x_+= " "
x_labels.append(x_)
charts = y_labels
charts.extend(x_labels)
print(charts)
return "\n".join(charts)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:146.0) Gecko/20100101 Firefox/146.0
Challenge Information:
Build a Budget App - Build a Budget App