Tell us what’s happening:
i cant pass the checker for the height for each bar should be rounded down to the nearest 10, that is exactly what i think my math does but it doesnt seem to be correct. i also passed all other tests. has anyone met with this problem before? thanks
Your code so far
from math import floor
class Category:
spend = 0
def __init__(self, name):
self.name = name
self.ledger =[]
self.balance = 0
def deposit(self, amount, description=''):
new_deposit = {
'amount': amount,
'description': description
}
self.ledger.append(new_deposit)
#self.balance += amount
def withdraw(self, amount, description=''):
if not self.check_funds(amount): return False
new_withdrawal = {
'amount': -amount,
'description': description
}
self.ledger.append(new_withdrawal)
#self.balance-=amount
return True
def get_balance(self):
total =0
for transaction in self.ledger:
total+=transaction['amount']
return total
def transfer(self, amount, other):
if not self.check_funds(amount): return False
self.withdraw(amount, f'Transfer to {other.name}')
other.deposit(amount, f'Transfer from {self.name}')
return True
def check_funds(self, amount):
if amount>self.get_balance(): return False
return True
def __str__(self):
title = (f"{self.name:*^30}\n")
menu = ""
for transaction in self.ledger:
menu+=(f"{transaction['description'][:23]:23}{transaction['amount']:7.2f}\n")
total = f"Total: {self.get_balance()}"
return (title + menu + total)
def create_spend_chart(categories):
for ctg in categories:
ctg.spend = 0
for item in ctg.ledger:
if item['amount'] < 0:
ctg.spend += -item['amount']
Category.spend += ctg.spend
# print(ctg.name, ctg.spend, Category.spend)
title = ('Percentage spent by category')
chart = ""
for cent in range(100,-1,-10):
chart += (f'\n{cent:3d}| ')
for ctg in categories:
ctg.percent = 100*ctg.spend/Category.spend
rounded = (ctg.percent//10)*10
if rounded >= cent:
chart += 'o '
else: chart+=' '
part = ' '
chart += f'\n{part:-<{len(categories)*3+5}}'
vertical = ''
names = [category.name for category in categories]
longest = max(len(name) for name in names)
for i in range(0,longest):
vertical += f'\n{part} '
for ctg in categories:
if i <= len(ctg.name)-1:
vertical += f'{ctg.name[i]} '
else : vertical += ' '
return (f'{title}{chart}{vertical}')
def main():
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
auto = Category('Auto')
auto.deposit(500)
# auto.withdraw(48)
food.transfer(50, clothing)
# clothing.withdraw(45)
# print(food)
print(create_spend_chart([food, clothing, auto]))
print(floor(food.percent), floor(auto.percent), floor(clothing.percent), (5//10))
if __name__ == '__main__':
main()
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App