Scientific Computing with Python Projects - Budget App

Tell us what’s happening:
In the create_spend_chart test, i’m getting an error. Everything is the same as expected except that the business category will have 0% spending according to the test whereas i’m getting 10%. Now going through the test_module.py, i found out that for that particular test total spending was 149.94. The withdrawal from business category was 10.99 which amounts to 7.33% rounded to 10%. So i don’t understand why is the test module using 0% as the expected output string. Only 1 test is failing and this is it. Please help me understand how can it be 0% instead of 10%.

Your code so far

class Category:
    ''' Objects of different expenditure categories belong to this class '''
    
    def __init__(self, category):
        
        self.category=category
        self.ledger=[]
        self.balance=0
    
    def deposit(self, amount, description=None):
        
        if description is None:
            description=''
        
        self.ledger.append({"amount" : amount, "description" : description})
        self.balance+=amount

    
    def withdraw(self, amount, description=None):
        
        if description is None:
            description=''
            
        if self.check_funds(amount):
            self.ledger.append({"amount" : -amount, "description" : description})
            self.balance-=amount
            return True
        
        else:
            return False
    
    def get_balance(self):
        
        return self.balance
    
    def transfer(self, amount, other):
        
        
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {other.category}")
            other.deposit(amount, f"Transfer from {self.category}")
            return True
        else:
            return False
    
    def check_funds(self, amount):
        
        if self.balance>=amount:
            return True
        else:
            return False
        
    def __str__(self):
        
        string=''
        space=30-len(self.category)
        string+="*"*(space//2)+self.category+"*"*(space//2)
        if len(self.category)%2==0:
            string+='\n'
        else:
            string+='*\n'
        
        for i in range(len(self.ledger)):
            
            string+=(self.ledger[i]['description']+" "*30)[:23]
            if isinstance(self.ledger[i]['amount'],int):
                
                spaces=4-len(str(self.ledger[i]['amount']))
                string+=' '*spaces+str(self.ledger[i]['amount'])+'.00'
            
            else:
                
                spaces=7-len(str(self.ledger[i]['amount']))
                string+=' '*spaces+str(self.ledger[i]['amount'])
            
            string+='\n'
        
        string+='Total: '+str(self.balance)
        return string
        
        
        
        
            

def create_spend_chart(categories):
    
    #lets calculate total withdrawl across categories and also each withdrawl
    wd=[]
    total=0
    for i in range(len(categories)):
        spending=0
        for item in categories[i].ledger:
            if item['amount']<0:
                spending+=abs(item['amount'])
        wd.append(spending)
        total+=spending
    
    #now find percentage in each case to nearest 10 
    for i in range(len(categories)):
        wd[i]=(wd[i]/total)*100 
        wd[i]=(wd[i]-(wd[i]%10)) if wd[i]%10<5.0 else (wd[i]+(10-wd[i]%10))
    #print(wd)
    
    #Now comes the plotting part
    string='Percentage Spent by category\n'
    for i in range(100,-10,-10):
        if i==0:
            string+= '  '+str(i)+'|'
        elif i<100:
            string+=' '+str(i)+'|'
        else:
            string+=str(i)+'|'
        for j in range(len(categories)):
            string+=" "+("o" if wd[j]>=i else " ")+" "
        string+='\n'
    
    string+=' '*4+'-'*3*len(categories)+'\n'
    #return string
    
    name=[item.category for item in categories]
    #print(name[0][2])
    for i in range(len(max(name, key=len))):
        string+=' '*4
        for j in range(len(categories)):
            string+=' '+(name[j][i] if i<len(name[j]) else ' ')+' '
        string+='\n'    
    return string   

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Budget App

Link to the challenge:

Hi, try to decide if ‘spending’ should include ‘transfer’.

1 Like

As far as i understood from the pytest module, they’re not doing any transfer. Everything is simple withdrawal. And i too had this question if transfer withdrawals are counted as withdrawal but i really have no idea about this. The problem statement just says, " The percentage spent should be calculated only with withdrawals and not with deposits". I don’t know what to make of it. If you or anyone else have already solved this question, please let me know what you guys implemented in this context.

Just think,
say if you include as transfer as spending.
Now in the category that receives the transfer use the transfer to buy something.

You are counting the some money twice in this case.

I understand this. I was also confused about this but the test module that is doing this part of the testing isn’t doing any transfer so things are pretty simple. That’s why i’m confused a little.

I’ve changed the code a little to not consider withdrawals if the word ‘Transfer’ occurs in the description. But it still gives an error since the test module didn’t even have any transfer operations.

with that fixed, you only need to re-read the requirement of how you decide how many ‘o’ for each category.

1 Like

Well, they asked for rounding it of to nearest 10 but that isn’t working. So i finally tried simply using a floor function and that led to passing of all the tests.
I just find it weird that they used floor function implementation for rounding to nearest 10.

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