My code prints the output that’s given in the example and the text aligns, but I can’t get the last check for the project. Could someone look over my code to tell me if there is an issue that I’m not seeing
Code:
class Category:
def __init__(self,category):
self.name = category
self.ledger = []
self.balance = 0
def deposit(self,amount,description=""):
self.balance += amount
self.ledger.append({"amount": amount, "description": description})
def withdraw(self,amount,description=""):
if self.check_funds(amount):
self.ledger.append({"amount": amount*-1, "description": description})
self.balance -= amount
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self,amount,other):
if self.withdraw(amount,f"Transfer to {other.name}"):
other.deposit(amount,f"Transfer from {self.name}")
return True
return False
def check_funds(self,amount):
if amount > self.balance:
return False
return True
def add_expenses(self):
expenses = 0
for i in self.ledger:
if i["amount"] < 0:
expenses += i["amount"] * -1
return expenses
def __str__(self):
string = ""
stars = 30 - len(self.name)
title_line = self.name
for i in range(0,stars):
if i < stars/2:
title_line = "*" + title_line
else: title_line += "*"
string += title_line + "\n"
for i in self.ledger:
dollar_amount = str("%.2f" % i["amount"])
if len(i["description"]) <= 23:
spaces = 30 - (len(i["description"]) + len(dollar_amount))
string += i["description"] + (" " * spaces) + dollar_amount + "\n"
else:
string += i["description"][:23] + " " + dollar_amount[:7] + "\n"
string += f"Total: {self.get_balance()}"
return string
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(5000,"deposit")
clothing.withdraw(45, "new shoes")
food.transfer(50, clothing)
print(food)
def create_spend_chart(categories):
string = "Percentage spent by category\n"
total_expenses = 0
char_lines = 0
for i in categories:
total_expenses += i.add_expenses()
if len(i.name) > char_lines:
char_lines = len(i.name)
percents = [["100|",100],[" 90|",90],[" 80|",80],[" 70|",70],[" 60|",60],[" 50|",50],[" 40|",40],[" 30|",30],[" 20|",20],[" 10|",10],[" 0|",0]]
for i in percents:
for j in categories:
percent = round((j.add_expenses()/total_expenses) * 10)
percent *= 10
if i[1] != 0:
if percent // i[1] > 0:
i[0] += " o "
else:
i[0] += " "
if i[1] == 0:
i[0] += " o "
for i in percents:
string += i[0] + "\n"
string += " " + ("---"*len(categories)) + "-\n "
for i in range(char_lines):
for j in categories:
if i < len(j.name):
string += f" {j.name[i]} "
else:
string += " "
string+= "\n "
return string
print(create_spend_chart([clothing,food]))