# Scientific Computing with Python Projects - Budget App

Okay, so I’ve solved most of the problem presented here and my code passes 10/11 tests. The one test that my code did not pass is the “create spend chart test.” I’ve checked my code against the example code presented within the main method and against the object data and can’t for the life of me figure out what exactly is going wrong or what data the test is comparing it to. Specifically, it seems the percentages in the test and the percentages I calculate don’t match. Can anyone help me figure out how to fix this?

class Category:

``````def __init__(self, name):
self.name = name
self.ledger = []

def __repr__(self):
rep_string = ""
len_ast = (30 - len(self.name)) // 2
rep_string = ("*" * len_ast) + self.name + ("*" * len_ast) + "\n"
for transaction in self.ledger:
trans_line = ""
if len(transaction["description"]) < 23:
trans_line += transaction["description"]
else:
trans_line += str(transaction["description"][:23])
amount_str = str("{:.2f}".format(transaction["amount"]))
if len(amount_str) > 7:
amount_str = str(str(amount_str)[:7])
spaces = " " * (30 - (len(trans_line) + len(amount_str)))
trans_line += spaces + amount_str + "\n"
rep_string += trans_line
total_line = "Total: " + str("{:.2f}".format(self.get_balance()))
rep_string += total_line
return rep_string

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):
balance = 0
for transaction in self.ledger:
balance += transaction["amount"]
return balance

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):
if self.get_balance() >= amount and self.get_balance() > 0:
return True
return False
``````

def create_spend_chart(categories):
# Initialize variables
num_categories = len(categories)
max_name = 0
for category in categories:
if max_name < len(category.name):
max_name = len(category.name)
bar_chart = “Percentage spent by category\n”
bar_y = [
" " * (3 - len(str(x))) + “{}|”.format(str(x))
for x in range(100, -10, -10)
]
bar_x = (" " * 4) + “-” * (3 * num_categories + 1)
names = [" " * 4 for category in categories for i in range(max_name)]

``````# Building chart
for category in categories:
cat_total_with = sum(
[dic["amount"] for dic in category.ledger if dic["amount"] < 0])
cat_total_dep = sum(
[dic["amount"] for dic in category.ledger if dic["amount"] > 0])
cat_spent_percent = round((abs(cat_total_with) / cat_total_dep) * 100,
-1)
#print("Category percentage: " + str(cat_spent_percent)) #Test

# Function seems to be working correctly, but is not reading all the withdraws and deposits
# Either that, or the instances of Category class are not storing them correctly

for i in range(len(bar_y)):
if int(bar_y[i].strip(" |o")) <= cat_spent_percent:
bar_y[i] += " o "
#print(bar_y[i]) # Test
else:
bar_y[i] += "   "

for idx in range(max_name):
if idx < len(category.name):
names[idx] += f" {category.name[idx]} "
else:
names[idx] += "   "

# Creating final output string
for y in bar_y:
while len(y) < 14:
y += " "
bar_chart += (y + "\n")

bar_chart += bar_x + "\n"

for i in range(len(names)):
if i == len(names) - 1:
bar_chart += "".join(names[i])
else:
bar_chart += "".join(names[i]) + " \n"

return bar_chart
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36`

Challenge: Scientific Computing with Python Projects - Budget App

Can you post the test output or replit link?

Here is the replit link: boilerplate-budget-app - Replit

It’s pretty confusing but the key line is here:

The percentage spent should be calculated only with withdrawals and not with deposits.

You are calculating the percentage spent out of the total deposited in each category.

You need to calculate the total \$ spent overall in all categories combined, and then what percent of that was spent in each category.

If \$10 is withdrawn from Food, \$20 is withdrawn from Entertainment and \$30 is withdrawn from Business, then you have \$60 total spending. Food is 16%, Entertainment is 30% and 50% is business (should add to ~ 100%).

Thank you! This clears up everything!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.