Tell us what’s happening:
I can’t understand what is being asked in the 3 and 4. And I don’t understand whats wrong with the rest?
Your code so far
from itertools import zip_longest
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def deposit(self, amount, description=''):
self.ledger.append(dict([('amount', amount),('description', description)]))
def withdraw(self, amount, description=''):
if self.check_funds(amount):
self.ledger.append(dict([('amount', -amount),('description', description), ('withdraw', True)]))
return True
return False
def get_balance(self):
balance = 0
for transaction in self.ledger:
balance += transaction.get('amount')
return balance
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f'Tranfer to {category.name}')
category.deposit(amount, f'Transfer from {self.name}')
return True
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
return True
def __str__(self):
header = f'\n{self.name:*^30s}'
body = ''
total = '\nTotal: ' + str(self.get_balance())
for transaction in self.ledger:
body += f"\n{transaction.get('description')[:23]:<23s}{'{:.2f}'.format(transaction.get('amount')):>7}"
return header + body + total
def create_spend_chart(categories):
tytle = 'Percentage spent by category'
body = ''
def balls(categories):
balls_per_cat = {}
total_spent = 0
for category in categories:
amount = 0
for transaction in category.ledger:
if transaction.get('withdraw', False):
amount += -transaction.get('amount')
total_spent += -transaction.get('amount')
balls_per_cat[category.name] = amount
for category in balls_per_cat:
balls = round(balls_per_cat[category] / total_spent * 10)
balls_per_cat[category] = f"o{'o'*balls:<10}"
return balls_per_cat
balls_per_cat = balls(categories)
for i in range(100,-1,-10):
body += f"\n{i:>3}|"
for ball in balls_per_cat:
body += f" {balls_per_cat[ball][-1:]} "
balls_per_cat[ball] = balls_per_cat[ball][:-1]
line = f"\n {len(balls_per_cat)*'---'}-"
names_ = []
for name in categories:
names_.append(name.name)
names = ''
for letter in zip_longest(*names_, fillvalue=' '):
names += f"\n {' '.join(letter)} "
chart = tytle + body + line + names
return chart
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, )
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(food)
print(clothing)
print(create_spend_chart([food, clothing]))
Your browser information:
User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App