Tell us what’s happening:
I can’t get it to return all the percentages.
ive been trying for way to long. ive tried asking and doing research
and tried to reflect off of other code.
i really dont understand. i have no idea what im missing. im stuck in a loop
i tried to move on to create the chart, but it just doesnt make sense to me.
i took a break from it and tried to do some research, but i came back and im still stuck.
Your code so far
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 the header text
header = 'Percentage spent by category'
#create the percentages down the left side
#a
total_withdraw = 0
#create a list for the category withdraws
c_w = {}
amounts = [transaction['amount'] for category in categories for transaction in category.ledger]
for amount in amounts:
if amount < 0:
total_withdraw += -amount
# Calculate withdrawals for each category
for category in categories:
withdrawal = 0
for transaction in category.ledger:
amount = transaction['amount']
if amount < 0:
withdrawal += -amount # Add the amount spent (negative for withdrawals)
c_w[category.name] = withdrawal
method = math.floor(( c_w[category.name]/total_withdraw)*100)
return c_w
#return f'{header}\n{name}: {method}'
food = Category('Food')
auto = Category('Auto')
food.deposit(1000, 'initial deposit')
auto.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)
food.withdraw(100.99)
clothing.withdraw(21.17)
auto.withdraw(200)
print(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/150.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App