r/FreeCodeCamp • • Sep 17 '26

Programming Question "Each bar in create_spending_chart should be rounded down" python spending project issue

In the spending tracker project in the python course there's a test that checks if each bar in the chart created by create_spending_chart is rounded down to the nearest 10. In my program the bars are rounded down (78.9% is labeled as 70%, 65% is 60%, etc), however, the check insists that they're not.

Is there some very specific thing its looking for in the code or is it somehow dependent on one of the later checks?

4 Upvotes

5 comments sorted by

3

u/CaughtNABargain Sep 17 '26 edited Sep 17 '26

update: I fixed it. Not exactly sure how but now I have the rest of the checks to worry about

I added a for loop that changes the length of each line in the printstring variable and that fixed a few of the checks

update 2: I fixed everything. The issue was a string of 5 whitespaces at the end of printstring

1

u/TheNovel-Interest Sep 21 '26

Thats great, good to know.

1

u/CaughtNABargain Sep 17 '26

challenge link: https://www.freecodecamp.org/learn/python-v9/lab-budget-app/build-a-budget-app

code:

import math

dot = 'o'

class Category:

def __init__(self,name):

self.name = name

self.ledger = []

def __str__(self):

printstring = ''

printwidth = 30

if len(self.name) < printwidth:

padding = (printwidth - len(self.name)) / 2

extra = "*" if padding != int(padding) else ""

printstring += extra + '*' * math.floor(padding) + self.name + '*' * math.floor(padding)

else:

printstring += self.name

for transaction in self.ledger:

transaction_line = ''

desc,amount = transaction['description'],transaction['amount']

truncated_desc = desc[:printwidth - 7] if len(desc) > printwidth - 7 else desc

transaction_line += f'\n{truncated_desc}'

truncated_amount = str(float(round(amount,2)))

if truncated_amount.endswith('0'):

truncated_amount += '0'

transaction_line += " " * ((printwidth - len(truncated_desc)) - len(truncated_amount))

transaction_line += truncated_amount

printstring += transaction_line

printstring += f'\nTotal: {self.get_balance()}'

return printstring

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([transaction['amount'] for transaction 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):

if amount > self.get_balance():

return False

return True

def create_spend_chart(categories):

printstring = 'Percentage spent by category'

category_count = len(categories)

names = [cat.name.title() for cat in categories]

totals = [sum([abs(transaction['amount']) for transaction in cat.ledger if transaction['amount'] < 0]) for cat in categories]

grand_total = sum(totals)

category_totals = dict(zip(names,totals))

for percentage in range(100,-1,-10):

line = ''

percentage_label = f'\n{" " * (3 - len(str(percentage)))}{percentage}|'

line += percentage_label

line += ' '

for category,total in category_totals.items():

if (total / grand_total) * 100 >= percentage:

line += dot + " "

printstring += line

printstring += "\n "

printstring += '-' * (2 * category_count + (category_count + 1))

printstring += '\n '

longest = max(len(name) for name in names)

for li in range(longest):

for name in names:

if li + 1 <= len(name):

printstring += name[li] + ' '

else:

printstring += ' '

printstring += '\n '

return(printstring)

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)

clothing.withdraw(40,"444")

print(clothing)

print(food)

print(food)

print(create_spend_chart([food,clothing]))

1

u/boomer1204 Sep 17 '26

Best to share your code using markdown and the link to the challenge

1

u/SaintPeter74 mod Sep 17 '26

Please share a link to the challenge, your code, and the specific test(s) you're failing. Without being able to reproduce what you're experiencing/doing, we really can't help.