Tell us what’s happening:
I dont know what i did wrong to get the last check, because visually its the same as in the example
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
self.ori_balance = 0
def __str__(self):
self.counter = 0
self.printing = ""
for i in self.ledger:
self.amounting = f"{self.ledger[self.counter]['amount']:.2f}"
self.printing += f"{self.ledger[self.counter]['description'][:23]:<23}{self.amounting:>7}\n"
self.counter += 1
return str(f"*" * int((30-len(self.name)) / 2) + f"{self.name}" + f"*" * int((30-len(self.name)) / 2) + "\n" + self.printing + "Total: " + str(self.balance))
def deposit(self, amount, description=""):
self.balance += amount
self.ori_balance += amount
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description=""):
if self.check_funds(amount):
self.balance -= amount
self.ledger.append({'amount': -amount, 'description': description})
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self, amount, category2):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category2.name}")
category2.deposit(amount, f"Transfer from {self.name}")
return True
else:
return False
def check_funds(self, amount):
if amount > self.balance:
return False
else:
return True
def create_spend_chart(categories):
counter = -1
output = " "
withdrawal = 0
percentages = {
10: "",
9:"",
8:"",
7:"",
6:"",
5:"",
4:"",
3:"",
2:"",
1:"",
0:"",
}
print("Percentage spent by category")
for i in categories:
withdrawal += (i.ori_balance - i.balance)
for i in categories:
percentage = int((i.ori_balance - i.balance) * 100 / withdrawal)
for j in range(int(percentage/10), -1, -1):
percentages[j] += " o "
for i in range(10, -1, -1):
print(f"{i}0|{percentages[i]}" if i == 10 else f" {i}0|{percentages[i]}" if i != 0 else f" {i}|{percentages[i]}")
print(f" " + "-" * (len(categories) * 3 + 1))
listo = []
for i in categories:
listo.append(str(i.name))
longest = ""
for i in listo:
if len(i) > len(longest):
longest = i
for j in range(len(longest)):
counter += 1
for i in categories:
try:
if i.name[counter] is not "":
output += i.name[counter] + " "
else:
output += " "
except:
output += " "
if counter < len(longest)-1:
output += "\n "
elif counter == len(longest):
output += ""
else:
break
print(output)
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')
auto = Category('Auto')
auto.deposit(2000, "hahh")
food.transfer(50, clothing)
clothing.deposit(400, "dad")
clothing.withdraw(200, "blah")
create_spend_chart([food, clothing, auto])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 OPR/109.0.0.0
Challenge Information:
Build a Budget App Project - Build a Budget App Project