Help - Budget challenge - hard time finding the error :)

Tell us what’s happening:
I see no difference with my output and what is asked by the challenge, I can’t get rid of the last test Fail

Your code so far

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

    def __repr__(self):
        return self.cat
        
    def __str__(self):
        l_cat = len(self.cat)
        self.header = format(self.cat, "*^30") + '\n'
        self.body = ''
        
        for i in range(len(self.ledger)):
            self.body += format(str(self.ledger[i]['description'])[:23],'<23') + str(format(self.ledger[i]['amount'],'>7.2f')) + '\n'
        self.footer = 'Total: ' + format(self.balance,'.2f')
        
        return (self.header+self.body+self.footer)
    
    def get_balance(self):
        self.balance = 0
        for i in range(len(self.ledger)): 
            self.balance += self.ledger[i]['amount']    
        return(self.balance)

    def check_funds(self, amount):
        if amount<=self.balance:
            return True
        else:
            return False 
        
        #used in withdraw and transfer
        
    def deposit(self, amount=0, description=""):
        item = {"amount": amount, "description": description}
        self.ledger.append(item)
        self.balance += item['amount']
#        print(self.ledger)
        
    def withdraw(self, amount=0, description=""):
        item = {"amount": amount*(-1), "description": description}      
       
        if self.check_funds(amount):
            self.ledger.append(item)
            self.balance += item['amount']
#            print(self.ledger)
            return True    
        else:
            return False    
    
    def transfer(self, amount, dest_cat):               
        from_cat = format(str(self.cat),"s")
        if self.check_funds(amount):
            self.withdraw(amount, "Transfer to "+repr(dest_cat))
            dest_cat.deposit(amount, "Transfer from "+from_cat)
        return self.check_funds(amount)
    
    def get_expenses(self):
        self.sum_exp = 0
        
        for i in range(len(self.ledger)): 
            if self.ledger[i]['amount']<0 :
                self.sum_exp += self.ledger[i]['amount']
        return(self.sum_exp)
    
    

    
def create_spend_chart(lista):
    expenses = {}
    exp_quota = {}
    quota = {}
    total_exp = 0
    
    for cat in lista:
        total_exp += cat.get_expenses()
    
    for cat in lista:
        expenses[cat.cat] = cat.get_expenses()
        exp_quota[cat.cat] = "{0:.0%}".format(((abs(cat.get_expenses()/total_exp)*100)//10)/10)
        quota[cat.cat] = int(abs(cat.get_expenses()/total_exp)*100//10*10)

    #print(quota)
    #--------------------------------------creo colonna side         
    side = []
    for i in range(11):  
        side.append(str(format(100-i*10,'>3'))+"| ")
    
    #--------------------------------------creo colonne categoria
    cats = []
    for cat in lista:
        bins = []
        for i in range(11): 
            if i*10 >= 100-quota[cat.cat]:
                bins.append("o  ")
            else:
                bins.append("   ")
                
        cats.append(bins)
        
    #--------------------------------------grafico   
    lines = []
    print("Percentage spent by category")
    for i in range(11):  
        string = ""
        for j in range(len(lista)):
            string += cats[j][i]
        lines.append(side[i]+string) 
#        lines.append(side[i]+cats[0][i]+cats[1][i]+cats[2][i])
        print(lines[i])
    
    
    #--------------------------------------creo asse x
    dashline =" "*4 + (len(lista)*3+1)*"-"
    print(dashline)
    
    #--------------------------------------descrizioni categorie
    desc_list = []
    max_len_desc = 0
    
    for cat in lista:
        if max_len_desc < len(cat.cat): 
            max_len_desc = len(cat.cat)    
    
    for cat in lista:
        desc = []
        cat_len = 0
        
        for char in cat.cat: 
                desc.append(char)
                cat_len+=1
                
        while cat_len < max_len_desc:
            desc.append(" ")
            cat_len+=1
        
        desc_list.append(desc)

    desc_lines = []

    for i in range(max_len_desc):
        string = ""
        for j in range(len(lista)):
            string += desc_list[j][i]+"  "
        desc_lines.append(" "*5 + string)
#        desc_lines.append(" "*5 + desc_list[0][i]+"  "+desc_list[1][i]+"  "+desc_list[2][i]+"  ")
        print(desc_lines[i])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0.

Challenge: Budget App

Link to the challenge:

Please provide the output from the test suite to help us find the error.

you’re right, sorry :smile:

here’s the output:

 python main.py
973.96
*************Food*************
initial deposit        1000.00
groceries               -10.15
restaurant and more foo -15.89
Transfer to Clothing    -50.00
Total: 923.96
***********Clothing***********
Transfer from Food       50.00
                        -25.55
Total: 24.45
Percentage spent by category
100|          
 90|          
 80|          
 70|          
 60| o        
 50| o        
 40| o        
 30| o        
 20| o  o     
 10| o  o  o  
  0| o  o  o  
    ----------
     F  C  A  
     o  l  u  
     o  o  t  
     d  t  o  
        h     
        i     
        n     
        g     
None
.Percentage spent by category
100|          
 90|          
 80|          
 70|    o     
 60|    o     
 50|    o     
 40|    o     
 30|    o     
 20|    o  o  
 10|    o  o  
  0| o  o  o  
    ----------
     B  F  E  
     u  o  n  
     s  o  t  
     i  d  e  
     n     r  
     e     t  
     s     a  
     s     i  
           n  
           m  
           e  
           n  
           t  
F.........
======================================================================
FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-budget-app/test_module.py", line 94, in test_create_spend_chart
    self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
AssertionError: None != 'Percentage spent by category\n100|      [384 chars] t  ' : Expected different chart representation. Check that all spacing is exact.

----------------------------------------------------------------------
Ran 11 tests in 0.009s

FAILED (failures=1)

Ok, the test says this. It means that you are returning None from your function that is being tested by

So lets look at the return value of the create_spend_chart function

Uh oh. This function is printing without returning.

I didn’t think it was mandatory but it does make sense eheh
thanks a lot :+1:

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