Tell us what’s happening:
Hi,
Could someone please point me in the right direction? Can’t seem to pass test 19 and 24. Appreciate it!
Your code so far
from math import floor
class Category:
def __init__(self, category):
self.category=category
self.ledger=[]
self.budget=0
withdraw=False
def deposit(self,amount,description=''):
newEntry={"amount": amount, "description": description}
self.budget+=amount
self.ledger.append(newEntry)
def withdraw(self, amount, description=""):
if self.check_funds(amount):
self.deposit(-amount,description)
return True
else:
return False
def get_balance(self):
return self.budget
def check_funds(self, amount):
return self.get_balance()>=amount
def transfer(self, amount, to_category):
if self.check_funds(amount):
self.budget-=amount
self.ledger.append({"amount": -amount, "description": f'Transfer to {to_category.category}'})
to_category.deposit(amount, f'Transfer from {self.category}')
return True
else:
return False
def __str__(self):
output=''
title = self.category.center(30,'*')
output+=title+'\n'
for item in self.ledger:
description = item['description']
amount = item['amount']
output += f'{description[:23]:<23}{amount:>7.2f}\n'
output+=f"Total: "
output+=format(self.budget, '.2f')
return output
def create_spend_chart(categories):
output=''
title = 'Percentage spent by category\n'
output+=title
percentage=[]
spacing=4*' '
for cat in categories:
percent=0
for item in cat.ledger:
if item['amount']>0:
percent+=item['amount']
percent=(percent-cat.budget)*100/percent
percentage.append(int(percent))
for i in range(10,-1,-1):
row=''
output+=f'{i*10:3}|'
for p in percentage:
row+=f' 0 ' if round(p/10)>=i else ' '
print(len(row)+2)
output+=row+' \n'
output+=spacing +(len(row)+1)*'-'
max_length= max(len(str(word.category)) for word in categories)
for i in range(max_length):
row=''
for category in categories:
word= str(category.category)
row+= " "+word[i]+" " if i<len(str(word)) else " "
output+="\n"+ spacing + row +" "
return output
food = Category("Food")
clothing = Category("Clothing")
auto = Category('Auto')
food.deposit(1000, "deposit")
food.withdraw(600, "groceries")
food.withdraw(165.60, "books")
food.withdraw(234.10, "books")
clothing.deposit(1000, "deposit")
clothing.withdraw(200, "groceries")
auto.deposit(1000, "deposit")
auto.withdraw(100, "groceries")
print(create_spend_chart([food,clothing,auto]))
print(food)
print(clothing)
print(auto)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App
