Tell us what’s happening:
I don’t like asking for help on this one but the code seems to be doing what is required but still fails the last two tests. I can’t see that my formatting is any different but perhaps it is or maybe I’m just not understanding exactly what is wanted.
Your code so far
import math
class Category:
def __init__(self, category):
self.ledger = []
self.category = category
self.total = 0
def __repr__(self):
header = self.category.center(30, '*')
output_string = header + '\n'
for item in self.ledger:
amount = item['amount']
amount_str = f'{amount:.2f}'
desc_str = (item['description'][:28 - (len(amount_str))] + '..') if len(item['description']) > 28 - (len(amount_str)) else item['description']
spaces = " " * (30 - (len(desc_str) + len(amount_str)))
output_string += f'{desc_str:<}{spaces}{amount_str:>}\n'
output_string += "Total: " + f'{self.total:.2f}'
return output_string
def deposit(self, amount, description=""):
self.total += amount
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description=""):
if self.check_funds(amount):
self.total -= amount
self.ledger.append({'amount': -amount, 'description': description})
return True
else:
return False
def get_withdrawls(self):
withdrawl_amt = 0
for i in self.ledger:
if i["amount"] < 0 and not i["description"].startswith("Transfer"):
withdrawl_amt += i["amount"]
return withdrawl_amt
def get_balance(self):
return self.total
def transfer(self, amount, instance):
if self.check_funds(amount):
self.total -= amount
self.ledger.append({'amount': -amount, 'description': 'Transfer to ' + instance.category})
instance.total += amount
instance.ledger.append({'amount': amount, 'description': 'Transfer from ' + self.category})
return True
else:
return False
def check_funds(self, amount):
return amount <= self.total
def get_total_withdrawls(categories):
total_withdrawls = 0
for category in categories:
total_withdrawls += category.get_withdrawls()
return total_withdrawls
def create_spend_chart(categories):
total_withdrawls = get_total_withdrawls(categories)
if total_withdrawls == 0:
return []
percent_per_cat = []
for c in categories:
percentage = (c.get_withdrawls() / total_withdrawls) * 100
rounded_percentage = math.floor(percentage / 10) * 10
percent_per_cat.append({c.category: rounded_percentage})
output_string = "Percentage spent by category\n"
for i in range(11):
output_string += str(100 - i * 10).rjust(3) + "| "
for percent in percent_per_cat:
if list(percent.values())[0] >= 100 - i * 10:
output_string += "o "
else:
output_string += " "
output_string += "\n"
output_string += " " + "-" * (len(categories) * 3 + 1) + "\n"
max_len = max(len(c.category) for c in categories)
for i in range(max_len):
output_string += " "
for c in categories:
if i < len(c.category):
output_string += c.category[i] + " "
else:
output_string += " "
output_string += "\n"
return output_string
food = Category('Food')
food.deposit(100, 'deposit')
food.withdraw(10, 'groceries')
food.withdraw(15, 'restaurant and more food for dessert')
clothing = Category('Clothing')
clothing.deposit(500, 'deposit')
clothing.withdraw(100, 'jeans')
auto = Category('Auto')
auto.deposit(500, 'deposit')
auto.withdraw(130, 'tyres')
rent = Category('Rent')
rent.deposit(500, 'deposit')
rent.withdraw(430, 'January')
categories = [food, clothing, auto, rent]
#print(food.get_withdrawls())
#print(clothing.get_balance())
print(food)
#print(f"Total withdrawals across all categories: {get_total_withdrawls(categories):.2f}")
spend_chart = create_spend_chart(categories)
print(spend_chart)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project