Whats Happening?
I have spent hours looking over the two errors persisting when running tests for my program. The console tells me that each bar should be rounded down to the nearest 10, when in fact this is exactly what is occuring. The only other error present the last test case (#24), stating that my program should print a different chart representation. As my output compares exactly to the examples listed, I am flustered. This does not feel fair. Even the website “text-compare” tells me my chart is exactly matching the example output!!
My Current Program:
'''
Copyright (c) 2024, Rye Stahle-Smith; All rights reserved.
freeCodeCamp: Budget App Project
December 29, 2024
Description: This class instantiates objects based on different budget categories.
Each budget category contains a ledger holding the various transactions that have taken place.
Upon printing a category; the title, transactions, and total balance will be displayed.
Outside of the class, a seperate function is included to return a bar chart containing the percentage spent in each category.
'''
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def __str__(self):
display = ''
# Display the title line
for i in range(30):
if i >= (15 - len(self.name) // 2) and i < 30 - (15 - len(self.name) // 2):
display += self.name[i - (15 - len(self.name) // 2)]
else:
display += '*'
display += '\n'
# Display the list of items in the ledger
for i in range(len(self.ledger)):
for j in range(30):
if j < 23 and j < (29 - len(str("{:.2f}".format(self.ledger[i]['amount'])))):
if j < len(self.ledger[i]['description']):
display += self.ledger[i]['description'][j]
else:
display += ' '
display += ' ' + str("{:.2f}".format(self.ledger[i]['amount']))
display += '\n'
# Display the category total
display += 'Total: ' + str("{:.2f}".format(self.get_balance()))
return display
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):
total = 0
for i in range(len(self.ledger)):
total += self.ledger[i]['amount']
return total
def transfer(self, amount, category):
if self.check_funds(amount):
self.ledger.append({
'amount': -amount,
'description': f'Transfer to {category.name}'
})
category.ledger.append({
'amount': amount,
'description': f'Transfer from {self.name}'
})
return True
return False
def check_funds(self, amount):
if amount <= self.get_balance():
return True
return False
def create_spend_chart(categories):
bar_chart = ''
cat_with = [0 for i in range(len(categories))]
cat_dep = [0 for i in range(len(categories))]
cat_percent = [0 for i in range(len(categories))]
if len(categories) <= 4:
# Compute the category totals (from withdrawals)
for i in range(len(categories)):
for j in range(len(categories[i].ledger)):
if categories[i].ledger[j]['amount'] < 0:
cat_with[i] += categories[i].ledger[j]['amount']
else:
cat_dep[i] += categories[i].ledger[j]['amount']
cat_with[i] = -cat_with[i]
# Compute the category percentages
for i in range(len(categories)):
cat_percent[i] = int(cat_with[i] / cat_dep[i] * 100) // 10 * 10
# Display the bar chart
bar_chart += 'Percentage spent by category\n'
for i in range(100, -10, -10):
if i == 100:
bar_chart += f'{i}|'
elif i == 0:
bar_chart += f' {i}|'
else:
bar_chart += f' {i}|'
for j in range(len(cat_percent)):
if cat_percent[j] >= i:
bar_chart += f' o '
else:
bar_chart += f' '
bar_chart += ' \n'
bar_chart += ' '
for _ in range(len(categories)):
bar_chart += '---'
bar_chart += '-\n'
# Compute the max length
max_length = 0
for i in range(len(categories)):
if max_length < len(categories[i].name):
max_length = len(categories[i].name)
# Display the category names
for i in range(max_length):
bar_chart += ' '
for j in range(len(categories)):
if i < len(categories[j].name):
bar_chart += f' {categories[j].name[i]} '
else:
bar_chart += ' '
if i != max_length - 1:
bar_chart += ' \n'
else:
bar_chart += ' '
return bar_chart
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(600, 'groceries and dining')
clothing = Category('Clothing')
clothing.deposit(1000, 'deposit')
clothing.withdraw(200, 'clothing and accessories')
auto = Category('Auto')
auto.deposit(1000, 'deposit')
auto.withdraw(100, 'car repairs and gas')
print(create_spend_chart([food, clothing, auto]))
My Browser Information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python - Build a Budget App Project