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
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