I’m a little confused as to why my code isn’t passing other than not ending with a newline character at the end which I’ll have to somehow find a work around too… but everything else seems like it’s working and providing the expected output
Your code so far
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Please update the message to include your code. The code was too long to be automatically inserted by the help button.
When you enter a code, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
```
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.current_balance = 0
def check_funds(self, amount):
if amount <= self.current_balance:
return True
else:
return False
def deposit(self, amount, description = ""):
self.ledger.append(f"amount: {amount}, description: {description}")
self.current_balance += amount
def withdraw(self,amount, description= ""):
#checks if its possible to withdraw the specified amount
if self.check_funds(amount):
self.ledger.append(f"amount: -{amount}, description: {description}")
self.current_balance -= amount
return True
else:
return False
def get_balance(self):
return self.current_balance
def transfer(self,amount, destination):
#checks if transfer is possible
if self.check_funds(amount):
self.ledger.append(f"Transfer to {destination.name}: -{amount}")
destination.ledger.append(f"Transfer from {self.name}: {amount}")
self.current_balance -= amount
destination.current_balance += amount
return True
else:
return False
def __str__(self):
title = self.name
descriptions = ""
#this loops finds the description of each transaction
for i in self.ledger:
#This handles the transfer description
if "Transfer" in i:
#separates the description from the amount
description = i.split(":")
#only takes 23 characters of the description and ouputs the dollar amount on the side
if len(description[0]) <= 23:
split = i.split(": ")
gap = 30 - len(description[0])
float_a = float(split[1])
rounded_a = f"{float_a:.2f}"
descriptions += f"{description[0]}{rounded_a.rjust(gap)}\n"
else:
descriptions += f"{description[0][0:24]}\n"
#This handles all other descriptions
if "description" in i:
#splitted description
splitted_d = i.split(", description: ")
amount = splitted_d[0]
splitted_a = amount.split("amount: ")
float_a = float(splitted_a[1])
rounded_a = f"{float_a:.2f}"
if len(splitted_d[1][0:]) >= 23:
gap = 30 - len(splitted_d[1][0:23])
descriptions += f"{splitted_d[1][0:23]}{str(rounded_a).rjust(gap)}\n"
else:
gap = 30 - len(splitted_d[1][0:])
descriptions += f"{splitted_d[1][0:]}{str(rounded_a).rjust(gap)}\n"
self.current_balance = float(self.current_balance)
return f"{title.center(30, '*')}\n{descriptions}Total: {self.current_balance:.2f}"
def create_spend_chart(categories):
spent = []
# Get spending per category
for category in categories:
total = 0
for item in category.ledger:
if "amount" in item:
amount = float(item.split(",")[0].split("amount: ")[1])
if amount < 0:
total += abs(amount)
spent.append(total)
total_spent = sum(spent)
# Convert to percentages rounded down to nearest 10
percentages = [(s / total_spent) * 100 for s in spent]
percentages = [int(p // 10) * 10 for p in percentages]
# Build chart
chart = "Percentage spent by category\n"
for i in range(100, -1, -10):
row = str(i).rjust(3) + "|"
for p in percentages:
row += " o " if p >= i else " "
chart += row + " \n"
# Bottom line
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
# Category names vertically
max_len = max(len(c.name) for c in categories)
for i in range(max_len):
row = " "
for c in categories:
row += ("" + c.name[i] + " ") if i < len(c.name) else " "
chart += row + " \n"
return chart
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)
print(food)
print(create_spend_chart([food, clothing]))
```
The deposit method should create a specific object in the ledger instance variable.
let’s see the related user story
A deposit method that accepts an amount and an optional description. If no description is given, it should default to an empty string. The method should append an object to the ledger list in the form of {'amount': amount, 'description': description}.
can you point out which part of your code is satisfying this requirement?
Now that I have reorganized it to pass dictionaries into the method instead of a list, which I must note has significantly simplified the code. I need a little help understanding what the create spend chart percent really is? is it:
A) The percentage of money you spent in specifically in that category like say for example I had 1000 dollars in food, but I spent 500 dollars of it and therefore the percent would be 50%.
B) I had 1000 dollars in both (by that I mean each of them have 1000) Food and clothing, and spent $500 in each of them. Would the percent actually be a calculation of how much of my spending went into each category. Following the mentioned example that would be 50% of my spending went to food and 50% of my spending went to clothing?