Tell us what’s happening:
I have no idea what is the problem with this code.
It raises and error from test 16 to 24, I need help with this code.
Your code so far
class Category:
def __init__(self,name):
self.name = name
self.ledger = []
self.balance = 0
self.percent = 0
self.spending = 0
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)== True:
self.balance -= amount
self.spending += amount
self.ledger.append({'amount': -amount,'description': description})
return True
return False
def get_balance(self):
return self.balance
def transfer(self,amount,other_category):
if self.check_funds(amount)== True:
self.withdraw(amount, f'Transfer to {other_category.name}')
self.spending -= amount
other_category.deposit(amount, f'Transfer from {self.name}')
return True
return False
def check_funds(self,amount):
return self.balance >= amount
def __str__(self):
title += self.name.center(30, '*')
lines = [title]
for item in self.ledger:
desc = item ['description'][:23]
amount = f"{item['amount']:>7.2f}"
lines.append(f'{desc:<23}{amount}')
total = f"{self.balance:.2f}"
lines.append(f"Total: {total}")
return "\n".join(lines)
def create_spend_chart(categories):
total_spent = sum(cat.spending for cat in categories)
percentages = []
if total_spent == 0:
percentages = [0] * len(categories)
else:
for cat in categories:
pct = int((cat.spending / total_spent) * 100)
pct = (pct // 10) *10
percentages.append(pct)
chart = ["Percentage spent by category"]
for y in range(100, -1, -10):
bars = ''
for pct in percentages:
if pct>= y:
bars+= "o "
else:
bars += " "
chart.append(f"{y:>3}| {bars}")
dashes = "-" *(3 *len(categories))
chart.append(" " + dashes)
max_len = max(len(cat.name) for cat in categories)
for i in range(max_len):
line = " "
for cat in categories:
if i< len(cat.name):
line += cat,name[i]
else:
line += " "
line += " "
chart.append(line)
return "\n".join(chart)
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 Edg/150.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App