Tell us what’s happening:
I was just wondering how I could pass the remaining test cases? The output looks identical to the expected output to me
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.current_balance = 0
def check_funds(self, amount):
if amount <= self.current_balance:
return True
else:
return False
def deposit(self, amount, description = ""):
self.ledger.append({"amount": amount, "description": description})
self.current_balance += amount
def withdraw(self,amount, description= ""):
#checks if its possible to withdraw the specified amount
if self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": description})
self.current_balance -= amount
return True
else:
return False
def get_balance(self):
return self.current_balance
def transfer(self,amount, destination):
#checks if transfer is possible
if self.check_funds(amount):
self.ledger.append({
"amount": -amount,
"description": f"Transfer to {destination.name}"})
destination.ledger.append({
"amount": amount,
"description": f"Transfer from {self.name}"})
self.current_balance -= amount
destination.current_balance += amount
return True
else:
return False
def __str__(self):
title = self.name
descriptions = ""
for i in self.ledger:
gap = 30 - len(i["description"])
if len(i["description"]) <= 23:
descriptions += i["description"]
decimal_amount = format(i["amount"], ".2f")
descriptions += f"{str(decimal_amount).rjust(gap)}\n"
else:
descriptions += i["description"][0:23]
decimal_amount = format(i["amount"], ".2f")
descriptions += f"{str(decimal_amount).rjust(7)}\n"
return f"{title.center(30, '*')}\n{descriptions}"
def create_spend_chart(categories):
spending_in_each = []
total_spent = 0
for category in categories:
category_total = 0
for i in category.ledger:
if i["amount"] < 0:
category_total += abs(i["amount"])
spending_in_each.append(category_total)
total_spent += category_total
percents = []
for spent in spending_in_each:
percent = (spent / total_spent) * 100
percents.append(int(percent // 10) * 10)
x_axis = [100,90,80,70,60,50,40,30,20,10,0]
#Returns the x axis with o's symbolizing the percentages
chart = "Percentage spent by category"
for i in x_axis:
chart += f"\n{str(i).rjust(3)}|"
for p in percents:
if p >= i:
chart += f" o "
#builds the bottom dashes
chart += "\n " + "-" * (len(categories) * 3 + 1)
chart += "\n"
max_len = max(len(category.name) for category in categories)
for i in range(max_len):
chart += " " # 5 spaces for alignment
for category in categories:
if i < len(category.name):
chart += category.name[i] + " "
else:
chart += " "
if i < max_len -1:
chart += "\n"
return chart
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.withdraw(10, 'gucci')
create_spend_chart([food,clothing])
print(food)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App
