Tell us what’s happening:
This is the Build a Budget App certification project. What exactly am I missing here? Looking at the terminal, it seems I have all the steps down, and it matches the example output pretty much exactly from what I can tell. Is there another tweak that needs to be made to satisfy the prompts. I’ll post my code so far, I’d appreciate if someone can point me in the right direction. Everything up until the create_spend_chart is working, it’s just steps 18 down that I can’t seem to get.
Your code so far
class Category:
def __init__(self,name):
self.name=name
self.ledger=[]
all_categories.append(self)
def deposit(self,amount,description=''):
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self,amount,description=''):
if self.check_funds(amount)==True:
self.ledger.append({'amount':-amount,'description':description})
return True
else:
return False
def get_balance(self):
Balance=0
for ledger_dict in self.ledger:
for amount in ledger_dict.values():
if isinstance(amount,(int,float)):
Balance+=amount
return Balance
def transfer(self,amount,other):
if self.check_funds(amount)==True:
self.withdraw(amount,description=f'Transfer to {other.name}')
other.deposit(amount,description=f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self,amount):
Balance=self.get_balance()
if amount>Balance:
return False
else:
return True
def __str__(self):
string=''
balance=0
#alt: string+=f'{self.name:*^30}'
multi=round((30-len(self.name))/2)
string+=f'*'*multi
string+=f'{self.name}'
if multi*2+len(self.name)==29:
string+=f'*'*multi
string+=f'*'
else:
string+=f'*'*multi
string+=f'\n'
for ledger_dict in self.ledger:
for key,val in ledger_dict.items():
if key=='amount':
if len(ledger_dict['description'])>23:
for i,l in enumerate(ledger_dict['description']):
if i<23:
string+=f'{l}'
string+=f'{"":<0}'
else:
string+=f'{ledger_dict["description"]:<23}'
if key=='description':
if len(str(ledger_dict['amount']))>7:
for i,n in enumerate(str(ledger_dict['amount'])):
if i<7:
string+=f'{n}'
string+='\n'
else:
#alt: string+='{:>7.2f}'.format(ledger_dict["amount"])
#alt: string+='\n'
string+=f'{ledger_dict["amount"]:>7.2f}\n'
balance+=(ledger_dict['amount'])
string+=f'Total: {balance:.2f}'
return string
all_categories=[]
#list of all Category objects; must be called before creating class objects, so that initializer has soemthing to work with
categ=Category('Stuff')
categ.deposit(1000.07,'initial deposit')
categ.withdraw(100.05,'abcdefghijklmnopqrstuvw')
categ.withdraw(500.00,'good eatin\'')
categ.withdraw(200)
#print(categ.check_funds(100))
#print(categ.check_funds(1000))
#print(categ.get_balance())
food=Category('Food')
food.deposit(900, 'deposit')
food.withdraw(45., 'milk, cereal, eggs, bacon, bread')
#print(food.get_balance())
#print(food.transfer(1000,categ))
#print(food.withdraw(1000))
#print(
food.transfer(300,categ)
#)
supplies=Category('Supplies')
supplies.deposit(550,'initial')
supplies.withdraw(322,'grocery run')
print(categ)
print(food)
print(supplies)
others=Category('others')
others.deposit(200)
others.withdraw(50)
def create_spend_chart(categories):
chart=''
chart+=('Percentage spent by category\n')
totalnum=0
categnum_list=[]
for category in categories:
categnum=0
for ledger_dict in category.ledger:
for val in ledger_dict.values():
if (isinstance(val,(float,int))) and val<0:
categnum+=val
totalnum+=val
categnum_list.append(categnum)
#this is counting neg. transfers as withdrawals
percentages=[]
for categnum in categnum_list:
percentages.append(round(((categnum/totalnum)*100),-1))
y_axis=[num for num in range(101)if num%10==0]
y_axis.reverse()
for num in y_axis:
chart+=f'{num:4}| '
for percentage in percentages:
if not num>percentage:
chart+='o '
else:
chart+=' '
chart+='\n'
chart+=' '*5
for category in categories:
chart+=f'---'
chart+='-\n'
lenlist=[]
for category in all_categories:
lenlist.append(len(category.name))
l=max(lenlist)
numlist=[num for num in range(0,l+1)]
n=0
for num in numlist:
#think invisible numbers on left margin of every row
chart+=' '*6
#this space exist in every row, for every 'number'
for category in all_categories:
#for each num, check the category for their names
for num,c in zip(numlist,category.name):
if num==n:
chart+=f'{c} '
#would enumerate be better? maybe...but regardless this makes sure only nth letter of each name is posted
if n>=len(category.name):
chart+=' '
#needed for when categ name is out of letters to leave space instead and move to next column
chart+='\n'
#head to next row and grab a new iterations of all the categ names
n+=1
#keeps track of what row we're heading to
return chart
print(create_spend_chart(all_categories))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App
