I don’t know what is the problem I’ve tried everything I can imagine, it is shown as supposed and doing correct calculations, It’s the last thing left to get my certification, please help.
Failed:create_spend_chart should print a different chart representation. Check that all spacing is exact.
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
else:
return False
def get_balance(self):
return sum(item["amount"] for item in self.ledger)
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
else:
return False
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
title = f"{self.name:*^30}\n"
items = ""
total = 0
for item in self.ledger:
items += f"{item['description'][:23]:23}" + f"{item['amount']:7.2f}" + '\n'
total += item['amount']
return title + items + "Total: " + str(total)
# SPEND CHART
#import math
def create_spend_chart(categories):
withdrawals = {category.name: sum(item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories}
total_withdrawals = sum(withdrawals.values())
percentages = {category.name: int((withdrawals[category.name] / total_withdrawals) * 10) * 10 for category in categories}
#percentages = {category.name: math.floor((withdrawals[category.name] / total_withdrawals) * 10) * 10 + 10 if (withdrawals[category.name] / total_withdrawals) % 1 >= 0.5 else math.floor((withdrawals[category.name] / total_withdrawals) * 10) * 10 for category in categories}
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
chart += f"{i:3}| "
for category in categories:
if percentages[category.name] >= i:
chart += "o "
else:
chart += " "
chart += "\n"
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
max_length = max(len(category.name) for category in categories)
for i in range(max_length):
chart += " "
for category in categories:
if i < len(category.name):
chart += category.name[i] + " "
else:
chart += " "
chart += "\n"
return chart
#EXAMPLE
import random
food = Category("Food")
food.deposit(random.randint(100, 1000), "initial deposit")
food.withdraw(random.randint(10, 100), "groceries")
food.withdraw(random.randint(10, 100), "restaurant and more food for dessert")
clothing = Category("Clothing")
clothing.deposit(random.randint(100, 1000), "initial deposit")
clothing.withdraw(random.randint(10, 100), "shirts")
clothing.withdraw(random.randint(10, 100), "pants")
auto = Category("Auto")
auto.deposit(random.randint(100, 1000), "initial deposit")
auto.withdraw(random.randint(10, 100), "gas")
auto.withdraw(random.randint(10, 100), "maintenance")
print(create_spend_chart([food, clothing, auto]))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Budget App