Tell us what’s happening:
getting the error. The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.
Your code so far
bar_heights = []
for spent in spent_by_category:
percentage = (spent / total_spent) * 100
bar_height = int(percentage // 10) * 10
bar_heights.append(bar_height)
for y in range(100, -1, -10):
row = f"{y:>3}|"
for height in bar_heights:
row += " o" if height >= y else " "
lines.append(row)
lines.append(" " + "-" * (len(categories) * 3 + 2))
max_name_len = max((len(cat.name) for cat in categories), default=0)
for i in range(max_name_len):
row = " "
for cat in categories:
row += f" {cat.name[i]}" if i < len(cat.name) else " "
lines.append(row)
return "\n".join(lines)
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)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36 Edg/155.0.0.0
Challenge Information:
Build a Budget App - Build a Budget App
GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-budget-app/5e44413e903586ffb414c94e.md at main · freeCodeCamp/freeCodeCamp · GitHub
ILM
2
please provide your full code, it is impossible to test with what you have provided
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
def __str__(self):
lines = [self.name.center(30, '*')]
for entry in self.ledger:
description = entry['description'][:23]
amount = f"{entry['amount']:.2f}"
lines.append(f'{description:<23}{amount:>7}')
lines.append(f'Total: {self.get_balance()}')
return '\n'.join(lines)
def deposit(self, amount, description = ''):
self.amount = amount
self.description = description
self.ledger.append({'amount': amount, 'description': description})
def get_balance(self):
balance = 0
for i in self.ledger:
balance += i['amount']
return balance
def withdraw(self, amount, description = ''):
self.ledger.append({
'amount': -amount, 'description': description
})
if amount < self.get_balance():
return True
else:
return False
def transfer(self, amount, category):
if not self.withdraw(amount, f'Transfer to {category.name}'):
return False
category.deposit(amount, f'Transfer from {self.name}')
return True
def check_funds(self, amount):
if amount > self.get_balance():
return False
return True
def create_spend_chart(categories):
lines = ["Percentage spent by category"]
spent_by_category = []
total_spent = 0
for category in categories:
spent = 0
for entry in category.ledger:
if entry["amount"] < 0:
spent += abs(entry["amount"])
spent_by_category.append(spent)
total_spent += spent
if total_spent == 0:
for y in range(100, -1, -10):
lines.append(f"{y:>3}|")
lines.append(" " + "-" * (len(categories) * 3 + 2))
max_name_len = max((len(cat.name) for cat in categories), default=0)
for i in range(max_name_len):
row = " "
for cat in categories:
row += f" {cat.name[i]}" if i < len(cat.name) else " "
lines.append(row)
return "\n".join(lines)
bar_heights = []
for spent in spent_by_category:
percentage = (spent / total_spent) * 100
bar_height = int(percentage // 10) * 10
bar_heights.append(bar_height)
for y in range(100, -1, -10):
row = f"{y:>3}|"
for height in bar_heights:
row += " o" if height >= y else " "
lines.append(row)
lines.append(" " + "-" * (len(categories) * 3 + 2))
max_name_len = max((len(cat.name) for cat in categories), default=0)
for i in range(max_name_len):
row = " "
for cat in categories:
row += f" {cat.name[i]}" if i < len(cat.name) else " "
lines.append(row)
return "\n".join(lines)
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)
ILM
4
in the browser console, you can see this useful output from the tests
AssertionError: 'Perc[31 chars] \n 90| \n 80| \n 70| o \n 60[234 chars] t'
!= 'Perc[31 chars] \n 90| \n 80| \n 70[343 chars] t '
the above is your code, the below is the expected, it looks like you have a spacing problem
dhess
5
Try testing like this to visualize the spacing:
food = Category('Food')
categories.append(food)
food.deposit(1000,'deposit')
food.withdraw(10.15,'groceries')
food.withdraw(15.89,'restaurant and more food for dessert')
clothing = Category('Clothing')
categories.append(clothing)
food.transfer(50, clothing)
clothing.deposit(800,'deposit')
clothing.withdraw(55.20,'jacket')
auto = Category('Auto')
categories.append(auto)
auto.deposit(500,'deposit')
auto.withdraw(42.39,'fuel')
auto.withdraw(78.62,'oil change')
print(create_spend_chart([food,clothing,auto]).replace(" ","."))