Tell us what’s happening:
So I’ve got the metod in class Category:
def calculate_total_withdrawals(self):
total_withdrawal = 0
for i in range(len(self.ledger)):
if self.ledger[i]['amount'] < 0:
total_withdrawal += self.ledger[i]['amount']
return total_withdrawal
which should return total_withdrawal which is an int, but in the function create_spend_chart I have the lines:
for i in categories:
full_withdrawals += Category(i).calculate_total_withdrawals
which give me the error:
File “/home/runner/FlawlessTechnoDebugging/budget.py”, line 73, in create_spend_chart
full_withdrawals += Category(i).calculate_total_withdrawals
TypeError: unsupported operand type(s) for +=: ‘int’ and ‘method’
I think that’s the problem, that it returns method, but i don’t get it why it does so
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = list()
def check_funds(self, amount):
funds = 0
for i in range(len(self.ledger)):
funds = funds + self.ledger[i]["amount"]
if funds < amount:
return False
else:
return True
def deposit(self, amount, description=''):
self.dep = dict()
self.dep["amount"] = amount
self.dep["description"] = description
self.ledger.append(self.dep)
def withdraw(self, amount, description=''):
self.withd = dict()
self.withd["amount"] = amount
self.withd["description"] = description
if self.check_funds(amount):
self.ledger.append(self.withd)
return True
else:
return False
def get_balance(self):
total_dep = 0
total_withd = 0
for i in range(len(self.ledger)):
total_dep += self.ledger[i]["amount"]
return total_dep
def transfer(self, amount, another_category):
a = self.withdraw(amount, f"Transfer to {another_category.name}")
b = another_category.deposit(amount, f"Transfer from {another_category.name}")
def __str__(self):
title = f"{self.name:*^30}\n"
items = ""
total = 0
for i in range(len(self.ledger)):
items += f"{self.ledger[i]['description'][0:23]:23}" + f"{self.ledger[i]['amount']:>7.2f}" + '\n'
total += self.ledger[i]['amount']
output = title + items + "Total: " + str(total)
return output
def calculate_total_withdrawals(self):
total_withdrawal = 0
for i in range(len(self.ledger)):
if self.ledger[i]['amount'] < 0:
total_withdrawal += self.ledger[i]['amount']
return total_withdrawal
def calculate_percentage(sum, number):
percentage = round(number/sum * 100, -1)
return percentage
def create_spend_chart(categories):
title = "Percentage spent by category\n"
full_withdrawals = 0
percentages = []
x_axis = []
y_axis = []
bar = []
for i in categories:
full_withdrawals += Category(i).calculate_total_withdrawals
for i in range(len(categories)):
percentages.append(calculate_percentage(full_withdrawals, Category(categories[i]).calculate_total_withdrawals()))
for i in range(10):
y_axis.append(str(int(i) * 10) + '|')
for per in percentages:
if per == i:
bar[percentages.index(per)] += 'o '
else:
bar[percentages.index(per)] += ' '
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36
.
Challenge: Budget App
Link to the challenge: