Tell us what’s happening:
I passed all checks, but stuck at the final function that displays the barchart (the final check). My chart seems to have met all the requirements, and I can’t figure out why it still didn’t pass. Maybe it’s the calculation of withdrawal percentages that I misunderstood?
Thank you =3
Your code so far
class Category:
def __init__(self, category):
self.category = category
self.ledger = []
def __str__(self):
ledger_str = self.category.center(30, '*') + '\n'
max_length = len(ledger_str)
for item in self.ledger:
description_text = item['description'].ljust(23, ' ') if len(item['description']) <= 23 else item['description'][:23]
amount_text = f"{item['amount']:.2f}".rjust(7,' ')
if len(amount_text) > 7:
amount_text = amount_text[0:7]
ledger_str += f"{description_text}{amount_text}\n"
ledger_str += f"Total: {sum(item['amount'] for item in self.ledger)}"
return ledger_str
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):
# Calculate balance by summing all amounts in the ledger
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.category}")
category.deposit(amount, f"Transfer from {self.category}")
return True
return False
def check_funds(self, amount):
if sum(item['amount'] for item in self.ledger) >= amount:
return True
return False
food = Category("Food")
food.deposit(1000)
food.withdraw(100)
clothing = Category("Clothing")
clothing.deposit(1000)
clothing.withdraw(170)
college = Category("College Fund")
college.deposit(500)
college.withdraw(250)
drink = Category('Drink')
drink.deposit(1000)
drink.withdraw(500)
def create_spend_chart(categories):
# Calculate total withdrawals and spending percentages
withdrawn_list =[]
total_spent = 0
for cat in categories:
category_withdrawn = sum(item['amount'] for item in cat.ledger if item['amount'] < 0)
withdrawn_list.append(category_withdrawn)
total_spent += category_withdrawn
percentages = [(x/total_spent)*1000//10 for x in withdrawn_list] #withdraw% = cat.withdraw / total_withdraw?
# Build the chart
chart = 'Percentage spent by category\n'
for i in range(100, -1, -10):
chart += f"{i}| ".rjust(5, " ")
for percent in percentages:
chart += 'o' if percent >= i else ' ' # Print 'o' if reach percentages, or ' ' if not
chart += ' '
chart += '\n'
# Dash line
chart += ' '*4 + '-' * (len(categories)*3 +1)+'\n'
# Print category names
max_name_length = max(len(cat.category) for cat in categories)
for i in range(max_name_length):
chart += ' ' # Padding before the category names
for cat in categories:
# Print the i-th character of each category's name, or a space if shorter
chart += cat.category[i] if i < len(cat.category) else ' '
chart += ' '
chart += '\n'
return chart
categories = [food, clothing, college, drink]
print(create_spend_chart(categories))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project