Tell us what’s happening:
in create_spend_chart function my output show [object Object] and i dont know if is because a internal error or my code is with something wrong.
Your code so far
class Category:
def __init__(self,name):
self.name = name
self.ledger = []
def deposit(self, amount, description=''):
self.ledger.append({'amount': amount,
'description': description})
def withdraw(self, amount, description=''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount,
'description': description})
return True
else:
return False
def get_balance(self):
return sum(map(lambda x: x['amount'], self.ledger))
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {category.name}')
category.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self, amount):
if amount > self.get_balance():
return False
else:
return True
def __str__(self):
text = self.name.center(30,'*')
for entry in self.ledger:
float_amount = f"{entry['amount']:.2f}"
text += f"\n{entry['description'][:23].ljust(23)}"
text += f"{float_amount[:7].rjust(7)}"
text += f"\nTotal: {self.get_balance()}"
return text
def create_spend_chart(categories):
def sum_withdraw(categorie):
total_withdraw = 0
for entry in categorie.ledger:
if entry["amount"] < 0:
total_withdraw += entry["amount"]
return total_withdraw
def round_down(number):
return math.floor(number) - (math.floor(number)%-10)
def max_len_name(categories):
max_len = 0
for categorie in categories:
if len(categorie.name) > max_len:
max_len = len(categorie.name)
return max_len
categories_total_withdraw = sum([sum_withdraw(categorie) for categorie in categories])
categories_data = []
for categorie in categories:
categories_data.append({
"name": categorie.name,
"percentage": round(abs(sum_withdraw(categorie))/abs(categories_total_withdraw),1)*100,
})
title = 'Percentage spent by category'
print(title)
# y axis labels inverted
graph = []
for percentage in range(0,110,10):
text_percentage = str(percentage)
graph.append(f"{text_percentage.rjust(3)}| ")
# add chart bars
for i in range(0,11):
for data in categories_data:
if (i*10) <= data['percentage']:
graph[i] += "o "
print("\n".join(graph[::-1]))
print(4*" " + 10 * "-")
# add legend to x axis
for i in range(max_len_name(categories)):
print(3*" ", end="")
for categorie in categories:
if len(categorie.name) > i:
print(" " + categorie.name[i], end="")
else:
print(3*" ", end="")
print("")
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.withdraw(35.40, "jeans pants")
create_spend_chart([food, clothing])
My console output:
Percentage spent by category
100|
90|
80|
70| o
60| o
50| o
40| o
30| o o
20| o o
10| o o
0| o o
----------
[object Object]
F [object Object]
C [object Object]
[object Object]
o [object Object]
l [object Object]
[object Object]
o [object Object]
o [object Object]
[object Object]
d [object Object]
t [object Object]
[object Object]
[object Object]
h [object Object]
[object Object]
[object Object]
i [object Object]
[object Object]
[object Object]
n [object Object]
[object Object]
[object Object]
g [object Object]
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:153.0) Gecko/20100101 Firefox/153.0
Challenge Information:
Build a Budget App - Build a Budget App