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, 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):
    def deposit(self, amount, description=None):
        if description is None:
        self.ledger.append({"amount" : amount, "description" : description})

    def withdraw(self, amount, description=None):
        if description is None:
        if self.check_funds(amount):
            self.ledger.append({"amount" : -amount, "description" : description})
            return True
            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
            return False
    def check_funds(self, amount):
        if self.balance>=amount:
            return True
            return False
    def __str__(self):
        if len(self.category)%2==0:
        for i in range(len(self.ledger)):
            string+=(self.ledger[i]['description']+" "*30)[:23]
            if isinstance(self.ledger[i]['amount'],int):
                string+=' '*spaces+str(self.ledger[i]['amount'])+'.00'
                string+=' '*spaces+str(self.ledger[i]['amount'])
        string+='Total: '+str(self.balance)
        return string

def create_spend_chart(categories):
    #lets calculate total withdrawl across categories and also each withdrawl
    for i in range(len(categories)):
        for item in categories[i].ledger:
            if item['amount']<0:
    #now find percentage in each case to nearest 10 
    for i in range(len(categories)):
        wd[i]=(wd[i]-(wd[i]%10)) if wd[i]%10<5.0 else (wd[i]+(10-wd[i]%10))
    #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)+'|'
        for j in range(len(categories)):
            string+=" "+("o" if wd[j]>=i else " ")+" "
    string+=' '*4+'-'*3*len(categories)+'\n'
    #return string
    name=[item.category for item in categories]
    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 ' ')+' '
    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/ 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.