Tell us what’s happening:
Hello, Could someone look into my code and give me a hint please, i got stuck here for a couple of days, but, i still can’t find the solution correctly for the create spend chart function.
Your code so far
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
self.budget = 0
#the deposit method
def deposit(self, amount, description=''):
if description is None:
return description
else:
self.ledger.append({'amount': amount, 'description': description})
self.budget += amount
#the withdraw method
def withdraw(self, amount, description=''):
if not self.check_funds(amount):
return False
else:
self.budget -= amount
self.ledger.append({'amount': -abs(amount), 'description': description})
return True
#the get balance method
def get_balance(self):
return self.budget
#the transfer method
def transfer(self, amount, other_budget):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {other_budget.category}')
other_budget.deposit(amount, f"Transfer from {self.category}")
return True
return False
#the check_funds methos
def check_funds(self, amount):
if amount > self.budget:
return False
return True
def __str__(self):
result = ''
title = self.category.center(30, '*')
result += f'{title} \n'
for item in self.ledger:
description = item["description"]
amount = item["amount"]
result += f"{description[:23]} {amount:7.2f}\n"
result += f"Total: {self.budget: .2f}"
return result
#create spend chart
def create_spend_chart(categories):
amount_spent = []
for cat in categories:
spend = 0
for item in cat.ledger:
if item["amount"] < 0:
spend += abs(item["amount"])
amount_spent.append(round(spend, 2))
#get the total amount spend
total = round(sum(amount_spent, 2))
#get the percentage for each category
percentage_spent = list(map(lambda amount: int((amount / total) * 100), amount_spent))
title = "Percentage spent by category \n"
chart = ""
for i in range(100, -1, -10):
chart += f"{str(i) + '|':>4}"
for percent in percentage_spent:
if percent >= i:
chart += ' o '
else:
chart += " "
chart += "\n"
footer = " " + "-" * ((len(categories) * 3) + 1) + "\n"
category_names = [category.category for category in categories]
max_length = max(len(name) for name in category_names)
all_names = [name.ljust(max_length) for name in category_names]
for i in range(max_length):
footer += " "
for name in all_names:
footer += name[i] + " "
footer += "\n"
return title + chart + footer.rstrip('\n')
food = Category('Food')
food.deposit(900, 'deposit')
food.withdraw(45.67, 'groceries')
food.withdraw(200, 'drink')
clothing = Category('Clothing')
clothing.deposit(1200, 'deposit')
clothing.transfer(200, food)
clothing.withdraw(300)
auto = Category('Auto')
auto.deposit(2000, 'deposit')
auto.transfer(500, clothing)
auto.withdraw(500, 'Sports')
# print(food)
print(create_spend_chart([food, clothing, auto]))
# print(auto)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project