Tell us what’s happening:
I’ve reading and searching for serveral hints, but still didn’t passed for a few numbers. Can somebody tell me somthing wrong ?
Your code so far
class Category:
def __init__(self, category):
self.category = category
self.total = 0
self.ledger = list()
def __repr__(self):
header = self.category.center(30, '*')
print(header)
for item in self.ledger:
amount = '%.2f' % item['amount']
desc = (item['description'][:28 - (len(amount))] + '..') if len(item['description']) > 28 - (len(amount)) else item['description']
spaces = " " * (30 - (len(desc) + len(amount)))
txt = f'{desc:<}{spaces}{amount:>}'
print(txt)
print("Total: " + '%2f' %self.total)
def deposit(self, amount, description = ""):
self.total += amount
self.ledger.append({'amount' : amount, 'description' : description})
def withdraw(self, amount, description = ""):
self.total -= amount
self.ledger.append({'amount' : -amount, 'description' : description})
return True
def get_balance(self):
return self.total
def transfer(self, amount, instance):
if self.check_funds(amount):
self.total -= amount
self.ledger.append({'amount' : -amount, 'description' : 'Transfer to ' + instance.category})
instance.total += amount
instance.ledger.append({'amount' : amount, 'description' : 'Transfer from ' + self.category})
return True
else:
False
def check_funds(self, amount):
if amount < self.total:
return True
else:
return False
food = Category("Food")
clothing = Category("Clothing")
food.deposit(100, "Initial deposit")
food.withdraw(10, "super deluxe coffee")
food.withdraw(50, "protein food")
clothing.deposit(60, "Initial deposit")
food.transfer(15, clothing)
clothing.withdraw(80, "all the clothes")
def create_spend_chart(categories):
names_list = []
withdraw_list = []
for category in categories:
names = category.category
names_list.append(names)
height = (len(max(names_list, key=len)))
padded = [word.ljust(height) for word in names_list]
w_total = 0
for item in category.ledger:
amount = item['amount']
if amount < 0:
w_total += amount
withdraw_list.append(w_total)
total = int(round(sum(withdraw_list)))
percentages = []
for x in withdraw_list:
per = x * 100 / total
per = round(per//10)*10
percentages.append(per)
chart = "Percentage spent by category\n"
for x in reversed(range(0, 110, 10)):
chart += f"{str(x) + '|':>4}"
for percent in percentages:
if percent >= x:
chart += " o "
else:
chart += " "
chart += ' \n'
chart += " "+("-" * ((len(names_list) + 2) * 2)) + '\n'
for row in zip(*padded):
chart += (' '+' '.join(row)) + ' \n'
return chart.rstrip("\n")
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


