Scientific Computing with Python Projects - Budget App

Tell us what’s happening:

The result is already the same as example, but it still error on create_spend_chart. I have already compared with this test : ( taken from freecodecamp github )
def test_create_spend_chart(self):
self.food.deposit(900, “deposit”)
self.entertainment.deposit(900, “deposit”)
self.business.deposit(900, “deposit”)
self.food.withdraw(105.55)
self.entertainment.withdraw(33.40)
self.business.withdraw(10.99)
actual = budget.create_spend_chart([self.business, self.food, self.entertainment])
expected = "Percentage spent by category\n100| \n 90| \n 80| \n 70| o \n 60| o \n 50| o \n 40| o \n 30| o \n 20| o o \n 10| o o \n 0| o o o \n ----------\n B F E \n u o n \n s o t \n i d e \n n r \n e t \n s a \n s i \n n \n m \n e \n n \n t "
self.assertEqual(actual, expected, ‘Expected different chart representation. Check that all spacing is exact.’)
`);
Please help.
The replit link is : budget-app - Replit

Your code so far

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

    def deposit(self,dep=0,desc=""):
        if dep > 0 :
            depo = {}
            # depo["type"] ='d'
            depo["amount"] = dep
            depo["description"] = desc
            self.balance += dep
            self.ledger.append(depo)

    def withdraw(self,damount=0,description=""):
        if self._check_funds(damount) :
            draw = {}
            self.balance -= damount
            # draw["type"] = 'w'
            draw["amount"] = -damount
            draw["description"] = description
            self.ledger.append(draw)
        else :
            return False
        
        return True
    
    def transfer(self,tamount,cat):
        if self._check_funds(tamount) :
            draw = {}
            # draw["type"] = 'w'
            draw["amount"] = -tamount
            draw["description"] = "Transfer to " + cat.category
            self.ledger.append(draw)
            self.balance -= tamount
            cat.deposit(tamount,"Transfer from " + self.category)
        else:
            return False

        return True

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

    def get_balance(self):
        return self.balance

    def __str__(self):
        title = f"{self.category:*^30}\n"
        items = ""

        for item in self.ledger:
            items += f"{item['description'][:23]:<23}" + f"{item['amount']:>7.2f}\n"

        result = title + items + f"Total: {self.balance:.2f}"
        return result

def create_spend_chart(categories):
    diag = []
    tspent = 0
    cspent = []
    clen =0
    result = "Percentage spent by category\n"
    for i in range(100,-1,-10):
        diag.append([i,'|'])

    for c in categories:
        tspent += c.total_spent()

    for c in categories:
        pspent = (c.total_spent()/tspent)*100
        clen = len(c.category) if clen < len(c.category) else clen
        pspent = (pspent//10)*10
        cspent.append([c.category.capitalize(),pspent])

    for i in diag:
        i[1] = f"{i[0]: >3}{i[1]}"
        for j in cspent:
            if i[0] <= j[1]:
                i[1] += ' o '
            else:
                i[1] += '   '
        result += i[1]+' \n'
        
    line = f"{' '*4}"
    for c in range(len(cspent)):
        line += f"{'-'*3}"
    line += '-'
    for c in cspent:
        c[0] += ' '*(clen-len(c[0]))
    
    result += line + '\n'
    # print(cspent)
    line = ""
    for i in range(clen):
        line +=f"{' '*4}"
        for j in cspent:
            line+=(f" {j[0][i]} ")
        if i < clen-1 : line += ' \n' 
        else: line += ' '

    result +=line
    return(result)

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Budget App

Can you share your output printed with repr()?

for example:

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)


print(repr(create_spend_chart([food, clothing])))

Below is the result of repr() :

*************Food*************
deposit                1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96
'Percentage spent by category\n100| o     \n 90| o     \n 80| o     \n 70| o     \n 60| o     \n 50| o     \n 40| o     \n 30| o     \n 20| o     \n 10| o     \n  0| o  o  \n    -------\n     F  C  \n     o  l  \n     o  o  \n     d  t  \n        h  \n        i  \n        n  \n        g  '

Really? I get no output printing create spend chart. In another editor I get an error.

AttributeError: 'Category' object has no attribute 'total_spent'

Please visit my replit link. I use that for generate the result from repr() or the codes as below :

class Category:
    def __init__(self,category):
        self.category = category
        self.balance = 0.0
        self.__deposit = 0.0
        self.ledger = []

    def deposit(self,dep=0.0,desc=""):
        if dep > 0 :
            depo = {}
            depo["amount"] = dep
            depo["description"] = desc
            self.balance += dep
            self.__deposit += dep
            self.ledger.append(depo)

    def withdraw(self,damount=0.0,description=""):
        if self.check_funds(damount) :
            draw = {}
            self.balance -= damount
            draw["amount"] = -damount
            draw["description"] = description
            self.ledger.append(draw)
        else :
            return False
        
        return True
    
    def transfer(self,tamount,cat):
        if self.check_funds(tamount) :
            draw = {}
            # draw["type"] = 'w'
            draw["amount"] = -tamount
            draw["description"] = "Transfer to " + cat.category
            self.ledger.append(draw)
            self.balance -= tamount
            cat.deposit(tamount,"Transfer from " + self.category)
        else:
            return False

        return True

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

    def get_balance(self):
        return self.balance

    def __str__(self):
        title = f"{self.category:*^30}\n"
        items = ""
        for item in self.ledger:
            items += f"{item['description'][:23]:23}" + f"{item['amount']:>7.2f}\n"
        result = title + items + f"Total: {self.balance:.2f}"
        return result
    
    def total_spent(self):
        return self.__deposit - self.balance 
    

def create_spend_chart(categories):
    diag = []
    tspent = 0
    cspent = []
    clen =0
    result = "Percentage spent by category\n"
    for i in range(100,-1,-10):
        diag.append([i,'|'])

    for c in categories:
        tspent += c.total_spent()

    for c in categories:
        pspent = (c.total_spent()/tspent)*100
        clen = len(c.category) if clen < len(c.category) else clen
        pspent = (pspent//10)*10
        cspent.append([c.category.capitalize(),pspent])

    for i in diag:
        i[1] = f"{i[0]: >3}{i[1]}"
        for j in cspent:
            if i[0] <= j[1]:
                i[1] += ' o '
            else:
                i[1] += '   '
        result += i[1]+' \n'
        
    line = f"{' '*4}"
    for c in range(len(cspent)):
        line += f"{'-'*3}"
    line += '-'
    for c in cspent:
        c[0] += ' '*(clen-len(c[0]))
    
    result += line + '\n'
    # print(cspent)
    line = ""
    for i in range(clen):
        line +=f"{' '*4}"
        for j in cspent:
            line+=(f" {j[0][i]} ")
        if i < clen-1 : line += ' \n' 
        else: line += ' '

    result +=line
    return(result)

def main():
  # food = Category("food")
  # entertainment = Category("entertainment")
  # business = Category("business")
  # food.deposit(900, "deposit")
  # entertainment.deposit(900, "deposit")
  # business.deposit(900, "deposit")
  # food.withdraw(105.55)
  # entertainment.withdraw(33.40)
  # business.withdraw(10.99)
  # print(food)
  # print(entertainment)
  # print(business)
  # print(create_spend_chart([business,food,entertainment]))
  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)

  print(create_spend_chart([food, clothing]))
  print(repr(create_spend_chart([food, clothing])))
  
if __name__=="__main__":
    main()

This updated code passes all tests for me.

Is it still failing for you?

Yess, thank you for your review…I don’t know why yesterday it was fail …Thanks a lot !

1 Like

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