Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

My chart is displaying correctly but not passing the tests. I suspect it’s a spacing issue, but can’t find the problem.

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
        self.balance = float(0)
        self.withdrawals = float(0)

    def __str__(self):
        display = f'{self.name.center(30, "*")}'
        for i in self.ledger:
            display_amount = "{:.{}f}".format(i["amount"],2)
            display += f'\n{i["description"][:23].ljust(23)}{display_amount.rjust(7)}'
        display += f'\nTotal: {"{:.{}f}".format(self.balance,2)}'
        return display

    def deposit(self, amount, description=''):
        self.ledger.append({
            'amount': amount,
            'description': description
        })
        self.balance += amount

    def withdraw(self, amount, description=''):
        if self.check_funds(amount):
            self.deposit(-amount, description)
            return True
        return False

    def get_balance(self):
        return self.balance

    def transfer(self, amount, target):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {target.name}')
            target.deposit(amount, f'Transfer from {self.name}')
            return True
        return False

    def check_funds(self, amount):
        if amount > self.balance:
            return False
        return True

def create_spend_chart(cat1, cat2=Category(''), cat3=Category(''), cat4=Category('')):
    cats = [cat1, cat2, cat3, cat4]
    names = [i.name for i in cats if i.name]
    longest = -1
    for i in range(0, len(names)):
        if len(names[i]) > longest:
            longest = len(names[i])
    display = "Percentage spent by category"

    #sum withdrawals in each category
    for i in cats:
        for j in i.ledger:
            if j['amount'] < 0 and j['description'].find('Transfer') == -1:
                i.withdrawals += j['amount']

    withdrawals = [i.withdrawals for i in cats if i.name]

    sum_all_categories = float(0)
    for i in range(0, 4):
        sum_all_categories += cats[i].withdrawals

    #generate bar chart
    for i in range(100, -1, -10):
        display += f'\n{str(i).rjust(3)}|'
        for j in withdrawals:
            if (j / sum_all_categories) * 100 > i:
                display += ' o '

    #horizontal line under bar chart
    display += "\n    -"
    for i in names:
        display += '---'

    #display category names    
    for i in range(1, longest + 1):
        display += '\n     '
        for j in names:
            if len(j) < i:
               display += '   '
            else:
               display += f'{j[i-1]}  '
    return display

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)
clothing.withdraw(30.25, 'shirts')
auto = Category('Auto')
auto.deposit(150, 'deposit')
auto.withdraw(25, 'gas')
auto.withdraw(20, 'overhaul')
clothing.withdraw(15.25, 't-shirts')
food.withdraw(125, 'office cafeteria')
#print(food)
#print(clothing)
#print(auto)
print(create_spend_chart(food,clothing,auto))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Please review this part of the instructions

create a function (outside of the class) called create_spend_chart that takes a list of categories as an argument.

1 Like