Tell us what’s happening:
Hey, im trying to get this done but i cants figure out how to fulfill the last step “create_spend_chart should print a different chart representation. Check that all spacing is exact.” i tried everything and im tired my head hurts please I NEED someone to help me !!!
Your code so far
class Category:
def __init__(self,name):
self.name = name
self.ledger = []
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 get_balance(self):
total = 0
for item in self.ledger:
total += item['amount']
return total
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {category.name}')
category.deposit(amount, f'Transfer from {self.name}')
return True
return False
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
title = f'{self.name:*^30}\n'
items = ""
for item in self.ledger:
description = item['description'][:23]
amount = f"{item['amount']:.2f}".rjust(7)
items += f'{description:<23}{amount}\n'
total = f'Total: {self.get_balance():.2f}'
return title + items + total
def create_spend_chart(categories):
total_spent = 0
spent_by_category = []
for category in categories:
spent = sum(-item['amount'] for item in category.ledger if item['amount'] < 0)
spent_by_category.append(spent)
total_spent += spent
percentages = [(spent / total_spent) * 100 for spent in spent_by_category]
chart = 'Percentage spent by category\n'
for i in range(100, -1, -10):
chart += f"{i:>3}| "
for percentage in percentages:
chart += "o " if percentage >= i else " "
chart += '\n'
chart += " " + "-" * (len(categories) * 4 + 1) + "\n"
longest_name = max(len(category.name) for category in categories)
for i in range(longest_name):
chart += " "
for category in categories:
if i < len(category.name):
chart += category.name[i] + " "
else:
chart += " "
chart += '\n'
return chart.rstrip()
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')
clothing.deposit(500, 'initial deposit')
clothing.withdraw(25.50, 'clothes')
food.transfer(50, clothing)
print(food)
print()
print(clothing)
print(create_spend_chart([food, clothing]))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project