Hey there,
at this point, I dont know what else I could try than ask for help, from what I understand, everything should be fine but it just wont accept it:
Project:
Failed:19. The height of each bar on the
create_spend_chart
chart should be rounded down to the nearest 10.
Failed: 23.
create_spend_chart
chart should have each category name written vertically below the bar. Each line should have the same length, each category should be separated by two spaces, with additional two spaces after the final category.
Failed: 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.
my attempt:
class Category:
categories = []
def __init__(self, category):
self.ledger = []
self.category = category
self.ledger_print = ""
def __str__(self):
return self.create_ledger_print()
def add_category(self):
if self not in Category.categories:
Category.categories.append(self)
return
def deposit(self, amount, description=""):
self.ledger.append({"amount": amount, "description": description})
self.add_category()
return
def withdraw(self, amount, description=""):
neg_amount = amount * -1
success = self.check_funds(amount)
if success:
self.ledger.append({"amount": neg_amount, "description": description})
return success
def transfer(self, amount, target_category):
success = self.check_funds(amount)
if success:
withdraw_description = f"Transfer to {target_category.category}"
target_category.add_category()
self.withdraw(amount, withdraw_description)
deposit_description = f"Transfer from {self.category}"
target_category.deposit(amount, deposit_description)
return success
def check_funds(self, amount):
success = False
if self.get_balance() >= amount:
success = True
return success
def get_balance(self):
return sum(item["amount"] for item in self.ledger)
def create_ledger_print(self):
ledger_print = [self.category.center(30, "*"), "\n"]
for item in self.ledger:
description = f"{item['description'][:23]:<23}"
amount = f"{item['amount']:.2f}"[:7]
ledger_print.append(description + f"{amount:>7}" + "\n")
ledger_print.append(f"Total: {self.get_balance():.2f}")
return "".join(ledger_print)
def gather_percentage(categories):
spendings = {}
percents = {}
percents_values = []
cats_upper = []
sum_spendings = 0
for category in categories:
amount = sum(item["amount"] for item in category.ledger if item["amount"] < 0) * -1
spendings[category.category] = amount
sum_spendings += amount
per_cent = sum_spendings / 100
for category, total in spendings.items():
percentage = int(max(0, (total / per_cent) // 10))
percents[category] = percentage
for item, key in percents.items():
percents_values.append(key)
cats_upper.append(str(item).upper())
return percents_values, cats_upper
def construct_seperator(categories):
seperator = [str(4 * " ")]
for _ in categories:
seperator.append(str(3 * "-"))
seperator.append("-\n")
target_length = len("".join(seperator))
return "".join(seperator), (target_length - 1)
def fix_length(target_row_length, row):
current_row_length = len("".join(row))
if current_row_length < target_row_length:
row.append(str((target_row_length - current_row_length) * " "))
return row
def construct_percent_chart(percents_values, target_row_length):
percent_chart = []
for i in range(100, -1, -10):
row = [f"{i:>3}|"]
for item in percents_values:
row.append(" o " if item >= (i // 10) else " ")
row = fix_length(target_row_length, row)
row.append("\n")
percent_chart.append("".join(row))
return "".join(percent_chart)
def create_cat_chart(cats_upper, target_row_length):
cat_chart = []
target_column_length = 0
for item in cats_upper:
if len(item) > target_column_length:
target_column_length = len(item)
for i in range(0, target_column_length):
row = []
for j, item in enumerate(cats_upper):
if j == 0:
row.append(str(4 * " "))
if i < len(item):
row.append(f" {item[i]} ")
else:
row.append(str(3 * " "))
row = fix_length(target_row_length, row)
if i < (target_column_length - 1):
row.append("\n")
cat_chart.append("".join(row))
return "".join(cat_chart)
def create_spend_chart(categories=Category.categories):
spend_chart = ["Percentage spent by category", "\n"]
percents_values, cats_upper = gather_percentage(categories)
seperator, target_row_length = construct_seperator(categories)
spend_chart.append(construct_percent_chart(percents_values, target_row_length))
spend_chart.append(seperator)
spend_chart.append(create_cat_chart(cats_upper, target_row_length))
return "".join(spend_chart)
Thanks in advance