Tell us what’s happening:
I dont get why my code fails the test the output matches completely
Printing a Category instance should give a different string representation of the object.
Heres what the output looks like
Your code so far
class Category:
def __init__(self,name):
self.ledger=[]
self.name=name
def __str__(self):
length=len(str(self.name))
start=(30-length)//2
header= '*'*start+str(self.name)+'*'*start
finals=""
for record in self.ledger:
if record['description']=="deposit":
desc="initial deposit"
else:
desc=record['description']
if len(desc)==23:
desc=record['description']
elif len(desc)>23:
desc=record['description'][:23]
else:
desc=desc+' '*(23-len(desc))
amts=(record['amount'])
if type(record['amount']) is float:
amt=(f" {amts}")
else:
amt=(f" {amts}.00")
if len(amt)==7:
amt=amt
elif len(amt)>7:
amt=amt[len(amt)-7:]
else:
amt=' '*(7-len(record['description']))+str(amt)
spacing=30-len(amt)-len(desc)
finals+= "\n"+ desc +" "*spacing+ amt
fund=str(self.get_balance())
if type(self.get_balance()) is float:
total=(f"{fund}")
else:
total=(f"{fund}.00")
total="\nTotal: " + total
return header + finals + total
def deposit(self,amount,description=''):
newEntry={"amount": amount, "description": description}
self.ledger.append(newEntry)
def get_balance(self):
balance=0
for record in self.ledger:
balance+=record["amount"]
return balance
def withdraw(self,amount,description=''):
withdrawal=False
newEntry={"amount": -amount, "description": description}
if self.check_funds(amount):
self.ledger.append(newEntry)
return True
else:
return False
def check_funds(self,amount):
if amount>self.get_balance():
return False
else:
return True
def transfer(self,amount,budget):
message1="Transfer to "+ budget.name
if self.check_funds(amount):
self.withdraw(amount,message1)
message2="Transfer from "+ self.name
budget.deposit(amount, message2)
return True
else:
return False
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")
food.transfer(50, clothing)
print(food)
def create_spend_chart(categories=[]):
pass
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Budget App
https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/budget-app
