Build a Budget App Test 16

Tell us what’s happening:

My code isn’t passing step 16 of this challenge:“Printing a Category instance should give a different string representation of the object.”

The output is identical (as far as I can tell) to the example.

Is there a real error in my code somewhere or is the fcc test just being picky?

Or is this one of those situations where the test won’t pass until I get further into the next part of the project?

Thanks!

Your code so far

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


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


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

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

        return(balance)


    def transfer(self, amount, other_cat):
        
        if self.check_funds(amount) == True:

            self.ledger.append({'amount': -amount, 'description': f"Transfer to {other_cat.name}"})

            other_cat.ledger.append({'amount': amount, 'description': f"Transfer from {self.name}"})
            
            return(True)                    
        else: 
            return(False)


    def check_funds(self, amount):
       
        if amount <= self.get_balance():
            return(True)
        else:
            return(False)

    def __str__(self):
        title = self.name
        output_lines = ""

        for entry in self.ledger:
            desc = entry['description'][:23]
            money = str(entry['amount'])[:7]

            fill_len = 30 - (len(desc) + len(str(money)))
            fill_str = " " * fill_len
            line = f"\n{desc}{fill_str}{money}"
            output_lines += line 

        return (f"{title.center(30, '*')} {output_lines}\nTotal: {self.get_balance()}")

food = Category("Food")
clothing = Category("Clothing")
auto = Category("Auto")

food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
food.transfer(50, clothing)


print(food)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App - Build a Budget App

Github Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-budget-app/5e44413e903586ffb414c94e.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @jjmckelvy

Both 1000 and -50 should contain two decimal places.

Happy coding

Derp!

Thanks - will try it out shortly!

Unfortunately there must be some other issue. I fixed the decimal problem (Which was actually non-trivial and worth the time) but there’s something else happening.

    def __str__(self):
        title = self.name
        output_lines = ""

        for entry in self.ledger:
            desc = entry['description'][:23]

# here's the new code to fix the decimal issue, and it's working
            money_to_dec = entry['amount']
            money = f"{money_to_dec:.2f}"[:7]

            fill_len = 30 - (len(desc) + len(str(money)))
            fill_str = " " * fill_len
            line = f"\n{desc}{fill_str}{money}"
            output_lines += line 

        return (f"{title.center(30, '*')} {output_lines}\nTotal: {self.get_balance()}")

Refactored the str method using .rjust and .ljust instead of adding in empty spaces between description and amount. Still getting the same (perfect) results in output, still throwing the step 16 error.

    def __str__(self):
        title = self.name
        output_lines = ""

        for entry in self.ledger:
            desc = entry['description'][:23]
            desc_pad = desc.ljust(23)

            money = f"{entry['amount']:.2f}"[:7]
            money_pad = money.rjust(7)


            
            line = f"\n{desc_pad}{money_pad}"
            output_lines += line
            
        return (f"{title.center(30, '*')} {output_lines}\nTotal: {self.get_balance()}")

Try removing the empty line before the initial deposit.

Happy coding

Found how the tests flagged an error by using F12 and I can figure out how to refactor w/o the output_lines variable, probably using .join.

New question: Would this error cause a problem in a real-world situation OR is it just an artifact of how the testing code works?

Thanks!

Hi @jjmckelvy

If the specs do not ask you to output an extra line, then a process downstream not expecting an extra line will fail.

Also, after you write the code and leave the company, others who rely on your code, and it could be a significant number of people and organisations, may find that someone removing the line because it shouldn’t have been there suddenly have software that doesn’t work.

Azer Koçulu unpublished his packages from the npm registry, including a library called left-pad. Turned out lots of developers and organisations had dependencies on that single library.

Happy coding

Cracked it.

The second issue was not an extra line, but one extra space ' ' in one of my lines. Tricky AF, because it did not impact the console output in any way.

Thanks for the help.