Tell us what’s happening:
cannot solve the code its gett
class Category():
def __init__(self, name):
self.name = name
self.ledger = []
self.total = 0.0
def deposit(self, amount, description = ""):
self.ledger.append({'amount': amount, 'description': description})
self.total += amount
def withdraw(self, amount, description = ""):
if not self.check_funds(amount):
return False
else:
self.total += (amount*-1)
self.ledger.append({'amount': round(amount*-1,2), 'description': description})
return True
def get_balance(self):
return self.total
def transfer(self, amount, budget):
if self.withdraw(amount,f'Transfer to {budget.name}'):
budget.deposit(amount,f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self, amount):
if amount > self.total:
return False
else:
return True
def __repr__(self):
outputstring = ''
outputstring += f"{self.name:*^30}\n"
end = 23
final = 0.0
for t in self.ledger:
if len(t['description']) > 23:
if type(t['amount']) == type(int()):
a = f"{t['amount']:.2f}"
outputstring += f"{t['description'][0:end]}{a:>{30 - len(t['description'][0:end])}}\n"
else:
outputstring += f"{t['description'][0:end]}{t['amount']:>{30 - len(t['description'][0:end])}}\n"
elif type(t['amount']) == type(int()):
a = f"{t['amount']:.2f}"
outputstring += f"{t['description'][0:end]}{a:>{30 - len(t['description'][0:end])}}\n"
else:
outputstring += f"{t['description']}{t['amount']:>{30 - len(t['description'])}}\n"
final += t['amount']
outputstring += f"Total: {final:.2f}"
return outputstring
food = Category('Food')
entertainment = Category('Entertainment')
business = Category('Business')
food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")
food.withdraw(105.55, 'for food')
business.withdraw(33.40, 'for business')
entertainment.withdraw(10.99, 'for entertainment')
#print(food.get_balance())
#print(str(food))
#print(food.ledger)
#print(business.ledger)
def create_spend_chart(categories = []):
total_withdraw = 0
catl = {}
chartstr = f"Percentage spent by category\n"
for c in categories:
ctotal = 0
for i in c.ledger:
if i['amount'] < 0:
total_withdraw += i['amount']
ctotal += abs(i['amount'])
catl[c.name] = ctotal
total_withdraw = abs(total_withdraw)
for key in catl:
percentage = (catl[key]/total_withdraw)*100
catl[key] = percentage
for p in range(100,-10,-10):
if p == 100:
chartstr += f"{str(p) + '|'}"
elif p == 0:
chartstr += f" {str(p) + '|'}"
else:
chartstr += f" {str(p) + '|'}"
for value in catl.values():
if p <= value:
chartstr += f' o '
else:
chartstr += f' '
chartstr += '\n'
L = len(catl.items())
chartstr += " "f"{'-'*(L*3 + 1)}"
chartstr += "\n"
categorylength = 0
for i in categories:
if len(i.name) > categorylength:
categorylength = len(i.name)
for i in range(categorylength):
chartstr += " "
for n in categories:
if i < len(n.name):
chartstr += ' ' + n.name[i] + ' '
else:
chartstr += " "
chartstr += " \n"
print(catl)
print(chartstr)
return chartstr
create_spend_chart([business, food, entertainment])
Your code so far
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project