Tell us what’s happening:
The last check on this says the format isn’t exact. I’ve checked the verbose output in the console multiple times after messing with the spaces, but I cant work out where to put the extra spaces and/or why they’re not showing up.
Your code so far
class Category:
balance=0
def __init__(self,category):
self.ledger = []
self.entries=[]
self.category=category
def check_funds(self,amount):
if amount<=self.balance:
return True
else:
return False
def deposit(self,amount,description=''):
if not description:
description=''
self.ledger.append({"amount": amount, "description": description})
self.balance+=amount
def withdraw(self,amount,description=''):
self.amount=-amount
if not description:
description=''
if self.check_funds(amount):
self.ledger.append({"amount": self.amount, "description": description})
self.balance+=self.amount
return True
else:
return False
def total_expenses(self):
money_out=0
for entry in self.ledger:
if entry["amount"]<0:
money_out+=-1*entry["amount"]
return money_out
def transfer(self,amount,new_cat):
if self.check_funds(amount):
new_cat.deposit(amount,f'Transfer from {self.category}')
self.withdraw(amount,f'Transfer to {new_cat.category}')
return True
else:
return False
def get_balance(self):
return (round(self.balance,2))
def __str__(self):
string=self.category.center(30,'*')
for entry in self.ledger:
num="{:.2f}".format(entry["amount"])
des=entry["description"]
if len(des)>=(31-len(str(num))):
des=des[0:29-len(str(num))]
line=f'\n{des}{str(num).rjust(30-len(des))}'
string+=line
string+=f'\nTotal: {round(self.balance,2)}'
return string
def create_spend_chart(categories):
total_out=0
percentage=0
result=''
lengths=[]
verticals=[]
chars=''
start_index=0
string='Percentage spent by category'+'\n'
for category in categories:
total_out+=category.total_expenses()
lengths.append(len(category.category))
max_length=max(lengths)
print("total out:",total_out)
for i in range(100,-1,-10):
if len(str(i))>=3:
index=(str(i)+'| ')
elif i==0:
index=' '+str(i)+'| '
else:
index=' '+str(i)+'| '
string+=index
for category in categories:
percentage=round((category.total_expenses()/total_out)*100,0)
if category==categories[0] and i<=percentage:
indicator='o '
elif category==categories[0] and i>percentage:
indicator=' '
elif i>percentage:
indicator=' '
else:
indicator=' o '
string+=indicator
string+='\n'
string+=' '+'-'*3*len(categories)+'-'
for category in categories:
string_cat=category.category
vertical=list(string_cat)
if len(category.category)<max_length:
vertical.extend(' ' for i in range(max_length-len(category.category)))
verticals+=vertical
for char in verticals:
if start_index>=max_length:
break
chars+='\n'+' '+verticals[start_index]+' '+verticals[start_index+max_length]+' '+verticals[start_index+2*max_length]
start_index+=1
if start_index>=max_length:
break
string+=chars
return string
food = Category("Food")
food.deposit(100,"lots of money many character")
food.withdraw(10,"two")
clothing = Category("Clothing")
clothing.deposit(30,"thing")
food.transfer(0,clothing)
clothing.withdraw(30,"your mum")
entertainment=Category("Entertainment")
entertainment.deposit(960,"why not")
entertainment.withdraw(8,"huh")
print(food)
print(clothing)
print(create_spend_chart([clothing,food,entertainment]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project