Tell us what’s happening:
Still can not pass . Should it use alignment of the categories?
Your code so far
class Category:
def __init__(self,name):
self.name=name
self.ledger=[]
def __str__(self):
title = f"{self.name:*^30}\n"
display=''
for entry in self.ledger:
desc = entry["description"][:23]
amount = entry["amount"]
display=display+f"{desc:<23}{amount:>7.2f}\n"
# Total line
total = self.get_balance()
balance=f"Total: {total:.2f}"
return title+display+balance.rstrip()
def deposit(self,amount,description=''):
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})
return True
return False
def get_balance(self):
return sum(entry["amount"] for entry in self.ledger)
def transfer(self,amount,category):
if self.check_funds(amount):
source=self
destination= category
source.withdraw(amount,f'Transfer to {destination.name}')
destination.deposit(amount,f'Transfer from {source.name}')
return True
return False
def check_funds(self, amount):
if amount>self.get_balance():
return False
return True
def view_chart_categ(categories):
cat_percentages = categories
#Create the chart category part
keys = list(cat_percentages.keys())
max_len = max(len(key) for key in keys)
categ=''
for i in range(max_len):
categ=categ+' '
for key in keys:
categ=categ+' '
if i < len(key):
categ=categ+key[i]+' '
else:
categ=categ+' '
categ=categ+' \n'
return categ.rstrip()
def view_bars(categories):
#Convert all float to in
int_data = {k: int(v) for k, v in categories.items()}
#Create a bar for each category
bar='o'
space=' '
bars=[]
for key,value in int_data.items():
value=round(value,-1) / 10
bars.append(bar*int(value+1)+space*(10-int(value)))
#Display chart with the bars
chart=''
for i in range(100,-1, -10):
o=int(i/10)
barchart=''
for bar in bars:
barchart+=bar[o]+' '
chart+=f'{i:3}| {barchart}\n'
return chart.rstrip()
def create_spend_chart(categories):
cat_withdraw={}
for cat in categories:
cat_withdraw[cat.name]=sum(entry["amount"] for entry in cat.ledger if entry["amount"] < 0) * -1
total_withdrawals=int(sum(cat_withdraw.values()))
#'Percentage spent by category'
total_percentages = dict(map(lambda item: (item[0], (item[1] / total_withdrawals) * 100), cat_withdraw.items()))
#Print the whole chart
print('Percentage spent by category')
print(view_bars(total_percentages))
#Print Horizontal line
print(f' {"-"*10}')
print(view_chart_categ(cat_withdraw))
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)
auto = Category("Auto")
auto.deposit(1000, "deposit")
auto.withdraw(50.00, "fuel")
food.transfer(200, clothing)
clothing.withdraw(100.00, "socks")
categories =[food,clothing,auto]
for categ in categories:
print(categ)
create_spend_chart(categories)
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Mobile Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App