Tell us what’s happening:
Okay, I know this is supposed to be a individual thing but I’m really stuck. I have been working on this for about 9 hours and most of that time I was trying to debug it. freeCodeCamp keeps telling me that my code “create_spend_chart should print a different chart representation.”
I think my output is correct but the test says otherwise.
I’ll upload the code below
Any help would be greatly appreciated…Thank you fellow coders!
Your code so far
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App Project - Build a Budget App Project
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.total = 0
def __str__(self):
title = f'{self.name:*^30}'
result = title + '\n'
for trans in self.ledger:
description = trans['description'][:23]
amount = round(trans['amount'], 2)
amount_str = str(amount)
if '.' not in amount_str:
amount_str += '.00'
elif amount_str[-1] == '.':
amount_str += '00'
elif amount_str[-1] == '0' and amount_str[-2] != '.':
amount_str += '0'
amount = str(amount_str)[:7]
if not description:
description = 'deposit'
whitespace = ' ' * (30 - len(description) - len(amount))
result += f'{description}{whitespace}{amount_str}\n'
total = round(self.get_balance(), 2)
total_str = str(total)
if '.' not in total_str:
total_str += '.00'
elif total_str[-1] == '.':
total_str += '00'
elif total_str[-1] == '0' and total_str[-2] != '.':
total_str += '0'
result += f'Total: {total_str}'
return result
def get_balance(self):
amount = 0
for i in range(len(self.ledger)):
amount += self.ledger[i]['amount']
return amount
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 * -1, 'description': description})
return True
else:
return False
def transfer(self, amount, new_category):
if self.withdraw(amount, f'Transfer to {new_category.name}'):
new_category.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self, amount):
return True if amount <= self.get_balance() else False
def get_spend_percentage(self):
deposit_total = 0
withdraw_total = 0
for trans in self.ledger:
if trans['amount'] > 0:
deposit_total += trans['amount']
else:
withdraw_total += trans['amount']
withdraw_total *= -1
return (withdraw_total / deposit_total) * 100
#I think this part is causing the me to fail the test
def create_spend_chart(categories):
result = 'Percentage spent by category\n'
for num in [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]:
label = f'{(3 - len(str(num))) * " "}{num}|'
row = [label]
for category in categories:
if round(category.get_spend_percentage() / 10) * 10 >= num:
row.append(' o ')
else:
row.append(' ')
#result.append(category.get_spend_percentage())
#print(''.join(str(result)))
row = ''.join(map(str, row))
result += f'{row}\n'
result += f' {(len(categories) * 3 + 1) * "-"}'
stage = 0
for i in range(100):
row = []
for category in categories:
try:
row.append(category.name[stage])
except IndexError:
row.append(' ')
if ''.join(row) != ' ' * len(categories):
result += f"\n {' '.join(row)}"
stage += 1
return result
food = Category('Food')
food.deposit(1000, 'initial deposit')
food.withdraw(780, 'groceries')
clothing = Category('Clothing')
clothing.deposit(500, 'initial deposit')
clothing.withdraw(100, 'jeans')
clothing.withdraw(50, 't-shirt')
entertainment = Category('Entertainment')
entertainment.deposit(300, 'initial deposit')
entertainment.withdraw(150, 'movie tickets')
entertainment.withdraw(100, 'concert')
travel = Category('Travel')
travel.deposit(800, 'initial deposit')
travel.withdraw(300, 'flight tickets')
travel.withdraw(200, 'hotel booking')
school = Category('School')
school.deposit(100)
print(create_spend_chart([food, clothing, entertainment]))
There should be a note in the console to press F12 and check for a long verbose error in the devtools console.
Did you try that? Can you share that error output?
system
Closed
April 11, 2025, 11:00pm
4
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.