Tell us what’s happening:
Please help me last 2 tests are not working in my code idk why
Your code so far
class Category:
def __init__(self,name):
self.ledger = []
self.balance = 0
self.name = name
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,dest):
if self.check_funds(amount)== True:
self.withdraw(amount,f'Transfer to {dest.name}')
self.spending-=amount
dest.deposit(amount,f'Transfer from {self.name}')
return True
return False
def check_funds(self,amount):
return self.balance>=amount
def __str__(self):
title = ''
title+=self.name.center(30,'*')
res = ''
res+= title +'\n'
total=0
for i in self.ledger:
d=i["description"][:23]
if not 'deposit' in res:
if 'Transfer' in i["description"]:
d = i[" description "]
a= f'{i["amount"]:.2f}'
l=f'{d}' +a.rjust(len(title)-len(d)) +'\n'
total+= i["amount"]
res +=l
t =f'Total: {total:.2f}'
res += t
return res
def create_spend_chart(categories):
s="Percentage spent by category\n"
total=0
cats={}
for cat in categories:
cat_total= 0
for item in cat.ledger:
amount=item['amount']
if amount < 0:
total+=round(amount)
cat_total+=round(amount)
cats[cat.name]=round(abs(cat_total))
total=abs(total)
for key,val in cats.items():
percent=round((val/total)*100)
cats[key]= percent
for n in range(100,-1,-10):
s+=str(n).rjust(3)+"|"
for val in cats.values():
if val>=n:
s+=" o "
else:
s+=" "
s+=" \n"
s+= " "+"-"*(len(categories)*3+1)+"\n"
m=max(len(c.name) for c in categories)
for i in range (m):
line= " "
for cat in categories:
try:
line+=f" {cat.name[i]} "
except IndexError:
line+= " "
line+= " "
s += line.rstrip() +"\n"
return s.rstrip()
food =Category("Food")
food.deposit(1000,"deposit")
food.withdraw(10.15,"groceries")
food.withdraw(15.89,"restaurant and more food for dessert")
clothing=Category("Clothing")
auto=Category("Auto")
food.transfer(50,clothing)
food.transfer(100,clothing)
clothing.withdraw(30.50,'t-shirt')
auto.withdraw(70.30,"engine")
print(food)
print(create_spend_chart([food,clothing,auto]))
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App