Build a Budget App Project - Which Percent calc Method

I can’t seem to get past the final test of this project
Looking at what the browser dev tools show me, I think I have a problem with calculating the raw percentages. The spec is not very clear, “The percentage spent should be calculated only with withdrawals and not with deposits.” What does this actually mean?

Thinking about this I can see two options:

  • Expenditure as a percentage of my budget for a category (how much of my budget have I spent) and
  • Expenditure of a category as a percentage of the total expenditure (What percentage of my total expenditure goes towards a particular budget category.

For my implementation I used the first method as follows:

  • (category expenditure / category budget) * 100
  • category expenditure is a sum of all negative amounts except where ledger.description starts with ‘Transfer to’
  • category budget is a sum of all positive amounts less negative amounts where ledger.description starts with ‘Transfer to’
  • a transfer is not and expenditure, but a transfer of budget from one expense category to another expense category. It therefore decreases the budget of the from category and increases the budget of the to category.
  • I added two methods budget() and expenditure() to the category class that implemented this

But now I’m wondering if “The percentage spent should be calculated only with withdrawals and not with deposits” implies the second method. Also going thru the Forum discussion I found this comment “You need to calculate the percentage spent in a category from the total spent”. All this makes me think I should be using the second method

Can somebody point me in the right direction :slight_smile:

P.S. Finding extra spaces and missing spaces is a tedious business but I must admit I learned a few new browser debugging tricks.

    def budget(self):
        amount = 0
        budget = 0
        for trans in self.ledger:
            desc = trans['description']
            amount = trans['amount']
            if amount > 0:
                budget += amount
            elif amount < 0 and desc[0 : 11 ] == 'Transfer to':
                budget += amount
            
        return budget

    def expenditure(self):
        amount = 0
        expenses = 0
        for trans in self.ledger:
            desc = trans['description']
            amount = trans['amount']
            if amount < 0 and not desc[0 : 11 ] == 'Transfer to':
                expenses += amount
         
        return expenses
def get_perc_data(categories):
    percent_list = []
    for trans in categories:
        budget = trans.budget()
        expenditure = trans.expenditure() * -1
        percent = (expenditure / budget) * 100
        round = (percent // 10) * 10
        percent_list.append(int(round))
    print(percent_list)
    return percent_list

This is correct, the 2nd method you mention.

Also keep in mind these values are to be rounded down to the nearest tenth.

A transfer method that accepts an amount and another budget category as arguments. The method should add a withdrawal with the amount and the description ‘Transfer to [Destination Budget Category]’. The method should then add a deposit to the other budget category

Also keep in mind that a transfer counts as a withdrawal and a deposit.

The percentage spent should be calculated only with withdrawals and not with deposits.

The chart works with all withdrawals, including transfers, which are withdrawals and should actually just use the withdrawal function.

Thanks for the feedback, I will adapt my code accordingly.

To me a transfer is not a withdrawal nor a deposit. It is a correction in the allocation of budget to expense categories (accounts). Technically it is implemented as a withdraw event and a deposit event. It is just a redistribution of funds. It cannot be reported as an expense.

But don’t mind me. It is just my accounting background getting in the way. :slight_smile:

1 Like