Tell us what’s happening:
Hi,
Running all tests with success except step 16. In trouble to identify why?
Looking at the output result seem to be ok.
Thanks,
Sergio
Your code so far
class Category:
def __init__(self,category):
self.ledger=[]
self.cat=category
def get_balance(self):
balance=0
for value in self.ledger:
balance+=value["amount"]
return balance
def __str__(self):
cat_string=self.cat.center(30,"*")
for line in self.ledger:
cat_string+=f"\n{line['description'][0:23].ljust(23)} "+f"{line['amount']:.2f}".rjust(7)
cat_string+=f"\nTotal: {self.get_balance():.2f}"
return cat_string
def check_funds(self,amount):
if amount>self.get_balance():
return False
return True
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
return False
def transfer(self,amount,category):
if self.check_funds(amount):
self.ledger.append({'amount':-amount,'description':f'Transfer to {category.cat}'})
category.ledger.append({'amount':amount,'description':f'Transfer from {self.cat}'})
return True
return False
def create_spend_chart(categories):
max=0
c_withdraws=[]
total_Cwithdraws=0
for c in categories:
c_withdraws.append(0)
if len(c.cat)>max:
max=len(c.cat)
for l in c.ledger:
if l["amount"]<0:
c_withdraws[len(c_withdraws)-1]+=abs(l["amount"])
total_Cwithdraws+=c_withdraws[len(c_withdraws)-1]
chart="Percentage spent by category"
for n in range(100,-10,-10):
chart+=f"\n{str(n).rjust(3)}| "
for _ in range(len(c_withdraws)):
if c_withdraws[_]*100/total_Cwithdraws>=n and c_withdraws[_]>0:
chart+="o "
else:
chart+=" "
chart+=" "
chart+="\n "
chart+="--".ljust(len(categories)*3,"-")
chart+="-"
for _ in range(max):
chart+="\n "
for c in categories:
if _<len(c.cat):
chart+=f" {c.cat[_]} "
else:
chart+=" "
chart+=" "
return chart
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.withdraw(30,'trousses')
print(food)
print(clothing)
print("\n")
print(create_spend_chart([food,clothing]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project