Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

My code can’t seem to pass the “Printing a Category instance should give a different string representation of the object.” test, although I have added a str method which alters the Category instances. Help please!

Your code so far

class Category:

    def __init__(self, name):
        self.name = name
        self.ledger = []

    def __str__(self):
        final_string = self.name.center(30, '*')
        for item in self.ledger:
            description_string = str(item['description'])[:23].ljust(23)
            amount_string = str(round(item['amount'], 2))[:7].rjust(7)
            final_string += '\n' + description_string + amount_string

        return final_string + '\n' + 'Total: ' + str(self.get_balance())

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

    def withdraw(self, amount, description=''):
        if self.get_balance() > abs(amount):
            self.ledger.append({'amount': -amount, 'description': description})
            return True
        else: return False

    def get_balance(self):
        balance = 0
        for expense in self.ledger:
            balance += expense['amount']
        return balance

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

    def check_funds(self, amount):
        budget = 10
        for item in self.ledger:
            budget += item['amount']
        return budget > amount


def create_spend_chart(categories):
    pass

test = Category('test')
test1 = Category('test1')
test.deposit(1000, '012345678901234567890123456789')
print(test.withdraw(1500, 'test'))
test.transfer(200, test1)
print(test)


Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Try using the example given in the instructions. Your output has a few differences.

Here is an example usage:

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)
print(food)

And here is an example of the output:

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

Your output:

*************Food*************
deposit                   1000
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing       -50
Total: 923.96
1 Like

I could not figure it out, tried everything. Then I F12 into developers’ tools. I found the string in the elements and saw that at the end after the total amount there was a single space. Added a space at the end of my display text and the test passed.

2 Likes

It’s crucial to check the dev tools area for this project.