Build a Budget App Project

Tell us what’s happening:

I have tried for days and cannot pass this test: “The transfer method should increase the balance of the category object passed as its argument.” I don’t know what I’m doing wrong. Also, I don’t know if my calculations for the percent chart is correct. I am supposed to be getting one ‘o’ for the ‘Business’ column but I’m getting two. I feel like I am correct but the module is telling me otherwise. Please help!

Your code so far

cats = []
class Category:
    def __init__(self, category):
        self.category = category
        self.ledger = []
        self.total = 0
        self.withdrawals = 0
        cats.append(self)

    def __str__(self):
        result = self.category.center(30, '*') + '\n'
        for item in self.ledger:
            descr_length = len(item['description'])
            if descr_length > 23:
                item['description'] = item['description'][:23]
                descr_length = len(item['description'])
            if (item['amount'] * 10) % 1 == 0 or item['amount'] == 0: # one decimal place, integers, or equal to zero
                item['amount'] = str(item['amount']) + '0'
            else:
                item['amount'] = item['amount'] # two decimal places
            if len(str(item['amount'])) > 7:
                item['amount'] = item['amount'][0:7]
            result += f"{item['description']}{item['amount']:>{30 - descr_length}}"
            result += '\n'
        self.total = round(self.total * 100) / 100
        result += f'Total: {self.get_balance()}'
        return result
    
    def deposit(self, amount, description = ''):
        amount = (round(amount * 100)) / 100 # for 3 or more decimal places
        self.ledger.append({'amount': amount, 'description': description})
        self.total += amount

    def withdraw(self, amount, description = ''):
        amount = (round(amount * 100)) / 100
        if self.check_funds(amount):
            self.ledger.append({'amount': amount/-1, 'description': description})
            self.total -= amount
            self.withdrawals += amount
            return True
        return False

    def get_balance(self):
        if (self.total * 10) % 1 == 0 or self.total == 0:
           return str(self.total) + "0"
        else:
            return self.total


    def transfer(self, amount, to):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {to.category}')
            to.deposit(amount, f'Transfer from {self.category}')
            return True
        else:
            return False
    
    def check_funds(self, amount):
        if amount > self.total:
            return False
        else:
            return True

def create_spend_chart(list_of_cats):
    if len(list_of_cats) > 4: return 'Please input max 4 categories'
    result = 'Percentage spent by category\n'
    x_axis = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]
    y_axis = []
    total_dashes = len(list_of_cats) + 7
    
    withdrawal_total = 0
    longest_cat = ''
    for cat in list_of_cats:
            withdrawal_total += cat.withdrawals
            if len(cat.category) > len(longest_cat):
                longest_cat = cat.category
    for cat in list_of_cats:  
        if withdrawal_total > 0:
            cat_percent = (cat.withdrawals / withdrawal_total) * 100
            cat_percent_tens = (round(cat_percent/10)) * 10
            #if cat_percent_tens <= 10: # these lines are necessary in order to pass the last test
                #cat_percent_tens = 0
            y_axis.append([cat.category, cat_percent_tens])
    print("Withdrawal total:" , withdrawal_total)
    print(y_axis)
    
    for num in x_axis:
        result += f'{num:>3}| '
        for percent in y_axis:
            if num > percent[1]:
                result += f'{" ":>3}'
            if num <= percent[1]:
                result += 'o  '
        result += '\n'
    
    result += '    ' # spaces before dashed line
    for dash in range(total_dashes):
        result += '-'
    result += '\n'
    
    for index in range(len(longest_cat)):
        result += '     '
        for cat in y_axis:
            try:
                result += f'{cat[0][index]}  '
            except:
                result += f'{" "}  '
        result += '\n'
    return f'{result.rstrip()}  '

food = Category("Food")
entertainment = Category("Entertainment")
business = Category("Business")    
food.deposit(900, "deposit")
entertainment.deposit(900, "deposit")
business.deposit(900, "deposit")

food.withdraw(105.55)
entertainment.withdraw(33.40)
business.withdraw(10.99)

print(create_spend_chart([business, food, entertainment]))

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Issue with transfer is caused by get_balance, which sometimes returns string and sometimes integer/float. It should be the latter for all cases.

Your chart is close. Take a closer look in the description to see how the values - bars on the chart should be rounded.

thank you so much! I guess the “get_balance” method simply requires the total balance to be returned without any special formatting. I had to address the formatting in the “str” method instead.

as far as the chart, those lines were necessary because of the “round down” instructions which I missed.

It’s weird because the test kept failing for the transfer method but it was the “get_balance” all along. I can’t see how they are related so it may be something they need to fix?

get_balance is used to confirm money for category was changed after the transfer. It probably would be good to make more explicit what might be expected from it. Would you be interested in creating issue in the freeCodeCamp’s GitHub repository? Sign in to GitHub · GitHub