Tell us what’s happening:
I compared and checked the number of spaces for the bar chart, the dashes, and the vertical titles and they all seem to be matching. Help? Not passing tests 21, 23, and 24 now. And I am also not really understanding what 24 is asking for.
Lastly, when I press F12 on my laptop, the wordings in the console is the same as that of the tests. I saw some people being able to bring up a different screen. Any tips? Thanks!
Edited to include tests:
[create_spend_chart] should correctly show horizontal line below the bars. Using three -
characters for each category, and in total going two characters past the final bar.
[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.
[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
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
def __str__(self):
# Find char_len of category
char_len = 0
for char in self.category:
char_len += 1
# Find length of *
line_len = 30
left_len = 0
right_len = 0
if (line_len - char_len) % 2 != 0:
left_len = (line_len - char_len) // 2
right_len = (line_len - char_len) // 2 + 1
else:
left_len = (line_len - char_len) // 2
right_len = (line_len - char_len) // 2
# Format main display body
line_body = ""
line_body_total = ""
for line in self.ledger:
description = line["description"][:23]
amount = f"{line['amount']:7.2f}"
desc_len = 0
for char in description:
desc_len += 1
amount_len = 0
for char in amount:
amount_len += 1
spaces = line_len - desc_len - amount_len
line_body = description + (" " * spaces) + amount
line_body = f"{line_body}\n"
line_body_total += line_body
# Format total display
total = self.get_balance()
total_display = f"Total: {total:.2f}"
display = ""
line_1 = ""
for i in range(left_len):
line_1 += "*"
line_1 = ("*" * left_len) + self.category + ("*" * right_len)
display = f"{line_1}\n{line_body_total}{total_display}"
return display
def deposit(self, amount, description=""):
self.ledger.append({"amount": amount, "description": description})
def withdraw(self, amount, description=""):
if self.check_funds(amount) == False:
return False
else:
amount = -amount
self.ledger.append({"amount": amount, "description": description})
return True
def get_balance(self):
balance = 0
for i in self.ledger:
balance += i["amount"]
return balance
def transfer(self, amount, to_category):
if self.check_funds(amount) == False:
return False
else:
amount = -amount
self.ledger.append({"amount": amount, "description": f"Transfer to {to_category.category}"})
to_category.ledger.append({"amount": abs(amount), "description": f"Transfer from {self.category}"})
return True
def check_funds(self, amount):
balance = self.get_balance()
if amount > balance:
return False
else:
return True
# Calculate expenses total (balance without deposit)
def expenses_total(self):
total = 0
for i in self.ledger:
if i["amount"] < 0:
total += abs(i["amount"])
return float(f"{total:.2f}")
def create_spend_chart(categories):
pass
# Calculate percentage in categories
category_expenses = [category.expenses_total() for category in categories]
category_percentages = [round((category/sum(category_expenses))*100) for category in category_expenses]
# Create chart
chart = ""
# Title
chart_title = "Percentage spent by category"
# left side labels
labels = [f"{num}|" if num == 100 else f" {num}|" if num < 100 and num > 0 else f" {num}|" for num in range(100, -1, -10)]
# Bar chart
line = ""
for label in labels:
line += f"{label} "
for category_percentage in category_percentages:
label = label.lstrip().replace("|", "")
if category_percentage >= int(label):
line += "o "
else:
line += " "
line += "\n"
# bottom line
bottom_line = " _" + "___" * len(categories) + "\n"
# Category titles
title_line = ""
category_titles = [category.category for category in categories]
max_len = max([len(category) for category in category_titles])
for i in range(0, max_len):
title_line += " "
for title in category_titles:
try:
title_line += f"{title[i]} "
except IndexError:
title_line += " "
if i < max_len - 1:
title_line += "\n"
else:
pass
chart = f"{chart_title}\n{line }{bottom_line}\n{title_line}"
return chart
food = Category("Food")
food.deposit(1000, "deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant and more food for dessert")
clothing = Category("Clothing")
clothing.deposit(300, "deposit")
food.transfer(50, clothing)
auto = Category("Auto")
auto.deposit(500, "deposit")
auto.withdraw(30.32, "gas")
auto.withdraw(320.10, "tires")
# print(food)
# print(auto)
# print(auto.expenses_total())
categories = [food, clothing, auto]
print(create_spend_chart(categories))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15
Challenge Information:
Build a Budget App Project - Build a Budget App Project