Tell us what’s happening:
I’ve been stuck in this project for a while, I’m not sure what should I fix.
I keep getting these issues :
19. The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.
24. create_spend_chart should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details.
Your code so far
import math
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
def __str__(self):
title_line = f"{self.name:*^30}\n"
ledger_lines = ""
for transaction in self.ledger:
description = transaction['description'][:23]
amount = f"{transaction['amount']:>7.2f}"
ledger_lines += f"{description:<23}{amount}\n"
total_line = f"Total: {self.balance:>.2f}"
return title_line + ledger_lines + total_line
def deposit(self, amount, description=''):
deposit = {'amount' : amount , 'description' : description}
self.ledger.append(deposit)
self.balance += amount
def withdraw(self, amount, description=''):
if self.check_funds(amount):
if not description:
description = ''
withdraw = {'amount' : -amount, 'description' : description}
self.ledger.append(withdraw)
self.balance -= amount
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self, amount, category):
if self.check_funds(amount):
withdrawal = {'amount' : -amount , 'description' : f'Transfer to {category.name}'}
self.ledger.append(withdrawal)
self.balance -= amount
deposit = {'amount': amount, 'description': f'Transfer from {self.name}'}
category.ledger.append(deposit)
category.balance += amount
return True
else:
return False
def check_funds(self, amount):
return amount <= self.balance
def create_spend_chart(categories):
total_spent = sum(
sum(transaction['amount'] for transaction in category.ledger if transaction['amount'] < 0)
for category in categories
)
category_spent = {
category.name: sum(transaction['amount'] for transaction in category.ledger if transaction['amount'] < 0)
for category in categories
}
percentages = {
name: (int((spent / total_spent) * 100) // 10) * 10 if total_spent > 0 else 0
for name, spent in category_spent.items()
}
chart = "Percentage spent by category\n"
for percentage in range(100, -1, -10):
chart += f"{percentage:>3}| "
for category in categories:
if percentages[category.name] >= percentage:
chart += "o "
else:
chart += " "
chart += "\n"
chart += " -" + "---" * len(categories) + "\n"
max_name_length = max(len(category.name)for category in categories)
for i in range(max_name_length):
chart += " "
for category in categories:
if i < len(category.name):
chart += category.name[i] + " "
else:
chart += " "
if i < max_name_length - 1:
chart += "\n"
return chart.rstrip("\n")
food = Category('Food')
food.deposit(900, 'deposit')
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')
clothing = Category("Clothing")
clothing.deposit(500, "deposit")
clothing.withdraw(150, "Jeans and shirts")
entertainment = Category("Entertainment")
entertainment.deposit(300, "deposit")
entertainment.withdraw(100, "Movies")
entertainment.withdraw(50, "Games")
print(food)
print(clothing)
print(entertainment)
print()
print(create_spend_chart([food, clothing, entertainment]))
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 Edg/131.0.0.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project