I pass all tests except the transfer method test, the test that should result in 854.33 and the create_spend_chart test. I have checked that the spacing is correct and don’t believe I have missed anything, and the results fort he other tests are what you would expect to see so I am stumped.
Any help would be appreciated.
P.S I am aware this is far from the most efficient way to do this.
Your code so far
class Category:
def __init__(self, category_name):
self.name = category_name
self.ledger = []
def __str__(self):
self.output_grid = '*'*round(((30-len(self.name))/2)) + self.name + '*'*round(((30-len(self.name))/2))
for transaction in self.ledger:
self.output_grid += "\n"
self.output_grid += transaction['description'][:min(23, len(transaction['description'])):]
self.output_grid += " " * (30-(len(str(format(transaction['amount'],".2f"))) + min(len(transaction['description']), 23))) + str(format(transaction['amount'], ".2f")[:min(7, len(str(format(transaction['amount'],".2f")))):])
self.output_grid += '\n' + f"Total: {self.get_balance()}"
return self.output_grid
def deposit(self, amount, description = ''):
self.ledger.append({'amount': float(format(amount, ".2f")), 'description': description})
def withdraw(self, amount, description = ''):
if self.check_funds(amount) == False:
return False
else:
self.ledger.append({'amount': float(format(0 - amount, ".2f")), 'description': description})
return True
def get_balance(self):
self.total = 0
for transaction in self.ledger:
self.total += float(transaction ['amount'])
return format(self.total, ".2f")
def transfer(self, amount, other_category):
if self.check_funds(amount) == False:
return False
else:
self.withdraw(amount, f'Transfer to {other_category.name}')
other_category.deposit(amount, f'Transfer from {self.name}')
return True
def check_funds(self, amount):
if float(self.get_balance()) < float(amount):
return False
else:
return True
def create_spend_chart(categories):
spent = {}
total = 0
for category in categories:
spent[category.name] = 0
for i in range(0, len(category.ledger)):
if category.ledger[i]['amount'] < 0:
spent[category.name] += round(category.ledger[i]['amount'], 2)
spent[category.name] = 0 - round(spent[category.name],2)
total += spent[category.name]
percentage = []
for sp in spent:
percentage.append(round((spent[sp] / total)*100,-1))
final_chart = 'Percentage spent by category\n'
for i in range(10,-1,-1):
if i == 10:
temp = str(i*10) + '|'
elif i < 10 and i > 0:
temp = " " + str(i*10) + '|'
else:
temp = " " + str(i*10) + '|'
for perc in percentage:
if perc >= i*10:
temp += ' o '
else:
temp += ' '
temp += ' \n'
final_chart += temp
final_chart += " " + "-"*(3*len(percentage)) + "-\n"
length = 0
for category in categories:
if len(category.name) > length:
length = len(category.name)
for i in range(0,length):
final_chart += " "
for category in categories:
if len(category.name) > i:
final_chart += category.name[i] + ' '
else:
final_chart += ' '
final_chart += "\n"
return final_chart
food = Category("Food")
food.deposit(900, 'deposit')
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')
print(food.get_balance())
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project