Build a Budget App - Build a Budget App

Tell us what’s happening:

All tests after 16 are failing. I have no idea what the problem seems to be. Output seems correct to me.

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
    
    def __str__(self):
        return_string = self.name[:30].center(30,'*') + '\n'
        for entry in self.ledger:
            return_string += entry['description'][:23].ljust(23) + "{:.2f}".format(entry['amount']).rjust(7) + '\n'
        return_string += f"Total: {self.get_balance():.2f}"
        return return_string
    

    def deposit(self, amount: float, description=''):
        self.ledger.append({'amount': amount, 'description': description})
    
    def withdraw(self, amount: float, description=''):
        if not self.check_funds(amount):
            return False
        self.ledger.append({'amount': -amount, 'description': description})
        return True

    def get_balance(self):
        balance = 0
        for entry in self.ledger:
            balance += (entry['amount'])
        return balance
    
    def check_funds(self, amount):
        if self.get_balance() < amount:
            return False
        return True
    
    def transfer(self, amount: float, destination: 'Category'):
        if not self.check_funds(amount):
            return False
        self.withdraw(amount, f'Transfer to {destination.name}')
        destination.deposit(amount, f'Transfer from {self.name}')
        return True

def create_spend_chart(categories):
    #20 lines
    category_withdrawals = {}
    total_withdrawal = 0
    for category in categories:
        withdrawals = 0
        for transaction in category.ledger:
            amount = transaction['amount']
            if amount < 0:
                withdrawals += amount
        category_withdrawals[category.name] = withdrawals
        total_withdrawal += withdrawals
    #calc percentages
    category_percentages = {}
    category_percentages_rounded = {}
    for name in category_withdrawals.keys():
        category_percentages[name] = category_withdrawals[name]/total_withdrawal
        category_percentages_rounded[name] = round(category_percentages[name]*10)*10
    output = "Percentage spent by category"
    return_string = ''
    for percent in range(100, -1, -10):
        line = '\n'+ f'{percent}|'.rjust(4)
        for value in category_percentages_rounded.values():
            char = ''
            if value >= percent:
                char = 'o'
            line += char.center(3,' ')
        return_string += line
    output += return_string
    return_string = '\n'
    return_string += "    " + "-" * (len(category_percentages_rounded) * 3 + 1)
    max_name_length = max(len(name) for name in category_percentages_rounded)
    for i in range(max_name_length):
        line = '\n    '
        for name in category_percentages_rounded:
            if i < len(name):
                line += name[i].center(3)
            else:
                line += '   '
        return_string += line
    output += return_string
    #output += create_graph(category_percentages_rounded)
    #output += create_legend(category_percentages_rounded)
    print(output)

def create_graph(percentages):
    return_string = ''
    for percent in range(100, -1, -10):
        line = '\n'+ f'{percent}|'.rjust(4)
        for value in percentages.values():
            char = ''
            if value >= percent:
                char = 'o'
            line += char.center(3,' ')
        return_string += line
    return return_string

def create_legend(percentages):
    return_string = '\n'
    return_string += "    " + "-" * (len(percentages) * 3 + 1)
    max_name_length = max(len(name) for name in percentages)
    for i in range(max_name_length):
        line = '\n    '
        for name in percentages:
            if i < len(name):
                line += name[i].center(3)
            else:
                line += '   '
        return_string += line
    return return_string

food = Category("Food")
food.deposit(1000, "deposit")
food.withdraw(500.00, "groceries")

clothing = Category("Clothing")
clothing.deposit(1000, "deposit")
clothing.withdraw(100.00, "buying new clothes")

auto = Category("Auto")
auto.deposit(1000, "deposit")
auto.withdraw(200.00, "fuel")

food.transfer(200, clothing)


categories = [food, clothing, auto]
chart_str = create_spend_chart(categories)

print(clothing)
print(chart_str)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.1 Safari/605.1.15

Challenge Information:

Build a Budget App - Build a Budget App

*************Food*************
initial deposit        1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96
*************Food*************
deposit                1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96

“initial depost” vs “deposit” stands out to me here

that part is working. The tests are failing with the Test for the output “Percentage spent by category”

after test 16, my mistake.

  1. You should have a function outside the Category class named create_spend_chart(categories) that returns a bar-chart string.

Review what your function should be returning

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.