Build a Budget App - Build a Budget App

Tell us what’s happening:

All tests for the chart fail but when I call the method it seems to work just fine. I have no clue what is going on

Your code so far

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

    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
        else:
            return False

    def get_balance(self):
        balance = 0
        for transaction in self.ledger:
            balance += transaction['amount']
        return balance

    def check_funds(self, amount):
        if amount <= self.get_balance():
            return True
        else:
            return False
    
    def transfer (self, amount, category_instance):
        if self.check_funds(amount):
            self.withdraw(amount,f'Transfer to {category_instance.name}')
            category_instance.deposit(amount, f'Transfer from {self.name}')
            return True
        else:
            return False
    
    def __str__(self):
        lines = [set_title(self.name)]
        for transaction in self.ledger:
            description = transaction['description'][:23]
            amount = '{:.2f}'.format(transaction['amount'])
            lines.append(f'{description.ljust(23)}{amount.rjust(7)}')
        lines.append(f'Total: {str(self.get_balance())[:8]}')
        return '\n'.join(lines)

def set_title(name):
    NLen = len(name)
    LPad = (30-NLen)//2
    RPad = round((30-NLen)/2)
    return '*'*LPad + name + '*'*RPad

def create_spend_chart(categories):
    chart_str = 'Percentage spent by category\n'
    withdraw_list = []
    separate_amount = []
    # Get the total amount spent
    for category in categories:
        for transaction in category.ledger:
            if transaction['amount']<0:
                withdraw_list.append(-transaction['amount'])
        separate_amount.append(round(sum(withdraw_list),2))
    total_spent = sum(withdraw_list)
    # Get the amount spent per category
    index = 0
    for value in separate_amount:
        if index == 0:
            pass
        elif index == 1:
            amnt = separate_amount[1] - separate_amount[0]
            separate_amount[1]=amnt
        elif index == 2:
            amnt = separate_amount[2] -separate_amount[1] -separate_amount[0]
            separate_amount[2] = amnt
            count = 0
            extra_amnt = 0
        index +=1
    # Get the percentages
    cat_1 = round((separate_amount[0]*100)//total_spent,2)
    cat_2 = round((separate_amount[1]*100)//total_spent,2)
    cat_3 = round((separate_amount[2]*100)//total_spent,2)
    percent_list = [cat_1,cat_2,cat_3]
    percentages = {}
    ix_2 = 0
    for name in percent_list:
        percentages.update({categories[ix_2].name : percent_list[ix_2]})
        ix_2 +=1
  
    # create the chart
    
    for x in range(100,-1,-10):
        line = f'{x:3}| '
        for value in percentages.values():
            if value >= x:
                line += 'o  '
        chart_str += line + '\n'
    chart_str += '    '+ '-'*10 

    # Print names Vertically
    longest_name = max(len(name) for name in percentages.keys())
    for i in range(0,longest_name):
        lines = '     '
        for name in percentages:
            if i < len(name):
                lines += name[i] + '  '
            else:
                lines += '   '
        chart_str  += '\n' + lines
    print(chart_str)



food= Category('Food')
clothing = Category('Clothing')
auto = Category('Auto')

food.deposit(1000, 'Initial Deposit')
food.withdraw(39.54, 'Restaurant')
food.withdraw(10.54, 'groceries')
food.transfer(200, auto)

clothing.deposit(600, 'Initial depo')
clothing.withdraw(25.76, 'Zara')
clothing.withdraw(67.39, 'shoes')

auto.deposit(500, 'Initial Depo')
auto.withdraw(300, 'mechanic')
auto.withdraw(56.38, 'gas')


create_spend_chart([food,clothing,auto])

Your browser information:

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

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

Welcome to the forum @drach3nland,

To see what your function returns in the console, wrap your function call with print.

You should have a function outside the Category class named create_spend_chart(categories) that takes a list of categories and returns a bar-chart string.

Does your function return anything?

Happy coding

You’re right, forgot that part. Thank you! Only solved one of all the issues though

If you need more help, please post your updated code, formatted as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor: