I cannot get tests 20 and 24 to pass, and I am not sure what is going wrong. I have cross-referenced with people with the same issue, and can’t get it to work. Can someone please help me understand
Your code so far
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
return False
def get_balance(self):
return sum(item["amount"] for item in self.ledger)
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
return self.get_balance() >= amount
def __str__(self):
title = f"{self.name:*^30}\n"
body = ""
for item in self.ledger:
body += f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"
return title + body + f"Total: {self.get_balance()}"
def create_spend_chart(categories):
result = "Percentage spent by category\n"
spent = []
for category in categories:
total = 0
for item in category.ledger:
if item["amount"] < 0:
total += -item["amount"]
spent.append(total)
total_spent = sum(spent)
percentages = [int((s / total_spent) * 100) // 10 * 10 for s in spent]
for level in range(100, -1, -10):
result += f"{level:>3}|"
for p in percentages:
result += " o " if p >= level else " "
result += "\n"
result += " " + "-" * (3 * len(categories) + 1) + "\n"
max_len = max(len(cat.name) for cat in categories)
for i in range(max_len):
result += " "
for cat in categories:
result += (cat.name[i] if i < len(cat.name) else " ") + " "
result += "\n"
return result[:-1]
food = Category('Food')
food.deposit(1000, 'deposit')
print(food.get_balance())
print(food.check_funds(1000))
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing = Category('Clothing')
clothing.deposit(500, 'deposit')
clothing.withdraw(100, 'suit')
auto = Category('Auto')
auto.deposit(10000, 'deposit')
auto.withdraw(50, 'maintainance')
categories = [food, clothing, auto]
bar_chart = create_spend_chart(categories)
bar_chart = bar_chart.replace(' ', '*')
print(bar_chart)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
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
return False
def get_balance(self):
return sum(item["amount"] for item in self.ledger)
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
return self.get_balance() >= amount
def __str__(self):
title = f"{self.name:*^30}\n"
body = ""
for item in self.ledger:
body += f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"
return title + body + f"Total: {self.get_balance()}"
def create_spend_chart(categories):
result = "Percentage spent by category\n"
spent = []
for category in categories:
total = 0
for item in category.ledger:
if item["amount"] < 0:
total += -item["amount"]
spent.append(total)
total_spent = sum(spent)
percentages = [int((s / total_spent) * 100) // 10 * 10 for s in spent]
for level in range(100, -1, -10):
result += f"{level:>3}|"
for p in percentages:
result += " o " if p >= level else " "
result += "\n"
result += " " + "-" * (3 * len(categories) + 1) + "\n"
max_len = max(len(cat.name) for cat in categories)
for i in range(max_len):
result += " "
for cat in categories:
result += (cat.name[i] if i < len(cat.name) else " ") + " "
result += "\n"
return result[:-1]
food = Category('Food')
food.deposit(1000, 'deposit')
print(food.get_balance())
print(food.check_funds(1000))
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing = Category('Clothing')
clothing.deposit(500, 'deposit')
clothing.withdraw(100, 'suit')
auto = Category('Auto')
auto.deposit(10000, 'deposit')
auto.withdraw(50, 'maintainance')
categories = [food, clothing, auto]
bar_chart = create_spend_chart(categories)
bar_chart = bar_chart.replace(' ', '*')
print(bar_chart)
however currently it is still throwing errors and I cannot find why. I think it is due to the fact that the code wants to have a second space after the dots but i only have one but as soon as I update that test 19 fails how would you go about solving this
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
return False
def get_balance(self):
return sum(item["amount"] for item in self.ledger)
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
return self.get_balance() >= amount
def __str__(self):
title = f"{self.name:*^30}\n"
body = ""
for item in self.ledger:
body += f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"
return title + body + f"Total: {self.get_balance()}"
def create_spend_chart(categories):
result = "Percentage spent by category\n"
spent = []
for category in categories:
total = 0
for item in category.ledger:
if item["amount"] < 0:
total += -item["amount"]
spent.append(total)
total_spent = sum(spent)
percentages = [int((s / total_spent) * 100) // 10 * 10 for s in spent]
for level in range(100, -1, -10):
result += f"{level:>3}|"
for p in percentages:
result += " o " if p >= level else " "
result += "\n"
result += " " + "-" * (3 * len(categories) + 1) + "\n"
max_len = max(len(cat.name) for cat in categories)
for i in range(max_len):
result += " "
for cat in categories:
result += (cat.name[i] if i < len(cat.name) else " ") + " "
result += "\n"
return result[:-1]
food = Category('Food')
food.deposit(1000, 'deposit')
print(food.get_balance())
print(food.check_funds(1000))
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing = Category('Clothing')
clothing.deposit(500, 'deposit')
clothing.withdraw(100, 'suit')
auto = Category('Auto')
auto.deposit(10000, 'deposit')
auto.withdraw(50, 'maintainance')
categories = [food, clothing, auto]
bar_chart = create_spend_chart(categories)
bar_chart = bar_chart.replace(' ', '*')
print(bar_chart)
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
return False
def get_balance(self):
return sum(item["amount"] for item in self.ledger)
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {category.name}")
category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
return self.get_balance() >= amount
def __str__(self):
title = f"{self.name:*^30}\n"
body = ""
for item in self.ledger:
body += f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"
return title + body + f"Total: {self.get_balance()}"
def create_spend_chart(categories):
result = "Percentage spent by category\n"
spent = []
for category in categories:
total = 0
for item in category.ledger:
if item["amount"] < 0:
total += -item["amount"]
spent.append(total)
total_spent = sum(spent)
percentages = [int((s / total_spent) * 100) // 10 * 10 for s in spent]
for level in range(100, -1, -10):
result += f"{level:>3}|"
for p in percentages:
result += " o " if p >= level else " "
result += "\n"
result += " " + "-" * (3 * len(categories) + 1) + "\n"
max_len = max(len(cat.name) for cat in categories)
for i in range(max_len):
result += " "
for cat in categories:
result += (cat.name[i] if i < len(cat.name) else " ") + " "
result += "\n"
return result[:-1]
food = Category('Food')
food.deposit(1000, 'deposit')
print(food.get_balance())
print(food.check_funds(1000))
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing = Category('Clothing')
clothing.deposit(500, 'deposit')
clothing.withdraw(100, 'suit')
auto = Category('Auto')
auto.deposit(10000, 'deposit')
auto.withdraw(50, 'maintainance')
categories = [food, clothing, auto]
bar_chart = create_spend_chart(categories)
bar_chart = bar_chart.replace(' ', '*')
print(bar_chart)
yes but no matter what I do the code refuses to fix that issue and as soon as i found a way to fix it the test 19 broke because it had 11 charachters per line