EDIT: This is my code with only 22 & 24 failing.
import math
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):
balance = 0
for transaction in self.ledger:
balance += transaction\['amount'\]
return balance
def check_funds(self, amount):
return amount <= self.get_balance()
def transfer(self, amount, destination_category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {destination_category.name}")
destination_category.deposit(amount, f"Transfer from {self.name}")
return True
else:
return False
def \__str_\_(self):
method = '\\n'.join(f"{transaction\['description'\]:23.23}{transaction\['amount'\]:>7.2f}" for transaction in self.ledger )
return f"{self.name.center(30,'\*')}\\n{method}\\nTotal: {self.get_balance()}"
def \__iter_\_(self):
for item in self.ledger:
return item
def create_spend_chart(categories):
###################################
#create a sum of all the withdraws from every category
total_withdraw = 0
amounts = \[transaction\['amount'\] for category in categories for transaction in category.ledger\]
for amount in amounts:
if amount < 0:
total_withdraw += -amount
#print(total_withdraw) # 658.0699999999999 #####theres a square grey box that appears in the comment behind this one. It appears on multiple comments, I commented the entire script to understand it better and it pops up throughout multiple parts. it doesn't effect anything it also disappears if you press the space button 2x like i did
#####################################
#create a dictionary that holds the sum of withdraws for each category
c_w = {}
for category in categories:
withdrawal = 0
for transaction in category.ledger:
if transaction\['amount'\] < 0:
withdrawal += -transaction\['amount'\]
c_w\[category.name\] = withdrawal
#print(c_w) #{'Food': 188.07, 'Clothing': 360, 'Auto': 110}
#######################################################################
#create a list of the category names
drop_down = \[\]
#create a dictionary of the category names and the percentage of the expenses of each category to the total of all expenses
percentages = {}
for Name, Amount in c_w.items():
percentage = math.floor((Amount/total_withdraw)\*100 / 10)\*10 #do the math do get the percentage. Divide by 10 to make it singe digits, so when you floor it it gets rid of the decimal and excess and you can multiply by ten to return it to the nearest 10 place BELOW it or DOWN specifically instead of the nearest one.
percentages\[Name\] = percentage
drop_down += \[Name\]
#print(percentages) #('Food', 30)\\n('Clothing', 50)\\n('Auto', 20)
#print(drop_down) #\['Food', 'Clothing', 'Auto'\]
###################################################
#create the inital body for the chart
chart = ''
header = 'Percentage spent by category'
chart += header + '\\n'
bottom_line = " " + "-" \* (len(c_w) \* 3 + 1) + '\\n'
#creating the y-axis
for num in range(100, -1, -10):
label = f"{num:3}| "
for Name in percentages.items():
#adding the bar details
if Name\[1\] >= num:
label += "o "
else:
label += " "
chart += label + "\\n"
chart += bottom_line
#print(chart) #comes back in the right shape, but with incorrect data
#create the vertical name display
#print(chart) #names are displayed correctly, but it still has the wrong data
longest_name_length = max(len(name) for name in drop_down)
for i in range(longest_name_length):
line = " "
for name in drop_down:
if i < len(name):
line += name\[i\] + " "
else:
line += ' '
chart += line + '\\n'
return chart
food = Category(‘Food’)
auto = Category(‘Auto’)
food.deposit(1000, ‘initial deposit’)
auto.deposit(1000, ‘initial deposit’)
food.withdraw(15.74, ‘groceries’)
food.withdraw(21.34, ‘restaurant and more food for dessert’)
clothing = Category(‘Clothing’)
food.transfer(50, clothing)
food.withdraw(100.99, ‘bada bing’)
clothing.withdraw(20, ‘ding dong’)
auto.withdraw(110)
clothing.deposit(1000, ‘shit’)
clothing.withdraw(340, ‘fuck’)
print(create_spend_chart([food, clothing, auto]))