Tell us what’s happening:
I’ve made everything as needed but any of tests (17-24) is not passed, even the title. and i don’t actually understand what i should to do in my browser console (all what i see in there is adblocker errors)
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def deposit(self, amount, descr=""):
if amount >= 0:
self.ledger.append({
'amount': amount,
"description": descr
})
return True
return False
def withdraw(self, amount, descr=""):
if self.check_funds(amount):
self.ledger.append({
'amount': amount * -1,
"description": descr,
})
return True
return False
def get_balance(self):
bal = 0
for i in self.ledger:
bal += i['amount']
return bal
def transfer(self, amount, dest):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {dest.name}")
dest.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
if amount <= self.get_balance():
return True
return False
def __str__(self):
title = f"{self.name:*^30}\n"
total = 0
full = ''
for i in self.ledger:
desc = i['description'][:23]
amount = i["amount"]
full += f"{desc:<23}{amount:>7.2f}\n"
total += amount
return title + full + f'Total: {total:.2f}'
def create_spend_chart(categories):
num = 0
percentages = [str(100 - 10 * i) for i in range(11)]
spendlist = [0 for i in range(len(categories)+1)]
maxchars = []
dots_n_separator = ""
charlist = []
#counting all spends in categories + sum of spends (last) and maximal length of name
for i in categories:
for n in i.ledger:
if "-" in str(n["amount"]):
spendlist[num] = round(spendlist[num] + n['amount'] * -1,2)
maxchars.append(len(i.name))
spendlist[len(categories)] += spendlist[num]
num += 1
maxchars = max(maxchars)
#turning spends into presentages and rounding
for i in range(len(spendlist)):
spendlist[i] = int(round((100 * spendlist[i])/spendlist[len(categories)], -1))
spendlist.pop()
spendlist.sort()
spendlist.reverse()
#-----creating table-----
print("Percentage spent by category")
#percentages
for y in range(11):
for i in range(len(spendlist)):
if spendlist[i] >= int(percentages[y]):
dots_n_separator += "o"
else: dots_n_separator += " "
dots_n_separator += " "
print(f"{percentages[y]:>3}" + "| " + f"{dots_n_separator}")
dots_n_separator = ""
#separating
for i in range(3 * len(categories)+1):
dots_n_separator += "-"
print(f" {dots_n_separator:>3}")
#sorting names of objects by amount
sorted_categories = sorted(categories, key=lambda obj: sum(item['amount'] for item in obj.ledger), reverse=True)
#creating lists of letters of names
sorted_categories = list(sorted_categories)
for i in range(0, len(sorted_categories)):
sorted_categories[i] = [word for word in sorted_categories[i].name]
#printing names
result = " "
for i in range(maxchars):
for word in range(0, len(sorted_categories)):
try:
result += f"{sorted_categories[word][i]}" + " "
except IndexError: result = result + " "
print(result)
result = " "
test = Category("Testing")
test.deposit(2000)
test.withdraw(500)
test.withdraw(500)
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(food)
create_spend_chart((food, test, clothing))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 OPR/124.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App