Build a Budget App Project - Build a Budget App Project

The code below is my code for “Build a Budget App Project”
it is failing in test #19 and #24 after F12 and looking at details it seems either failing in spacing correctly (which I don’t see where error is ? as far as I can see I spaced correctly) OR I am not calculating single digit percentage correct( for round down to the nearest 10 I round to the smallest 10, which will be 0 in case of single digit)

Would You please point out where I am doing wrong? thank you

Tell us what’s happening:

class Category:

    def __init__(self, cat):
        self.cat = cat
        self.ledger =[]

    def __str__(self):
        total = 0
        len_stars = 30 - len(self.cat)
        left = len_stars // 2 
        right = len_stars - left 
        title = left * '*' + str(self.cat) + right * '*' + '\n'
        for i in range(len(self.ledger)):
            amount = self.ledger[i]['amount']
            total += amount
            amount = (f'{amount:.2f}')[:7]
            description = (self.ledger[i]['description'])[:23]
            amount_place = 30 - len(description)
            title += description + amount.rjust(amount_place,' ')+'\n'
        title += 'Total: ' + f'{total:.2f}'
        return (title)
        
    def deposit(self, amount, description =''):
        self.ledger.append({'amount': amount, 'description': description})
        return

    def withdraw(self, amount, description =''):
        if self.check_funds(amount):
            self.ledger.append({'amount': amount*-1, 'description': description})
            return True
        else:
            return False

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

    def transfer(self, amount, category_obj):
        if self.check_funds(amount):
            description_to = 'Transfer to ' + category_obj.cat
            description_from = 'Transfer from ' + self.cat
            self.withdraw(amount, description_to)
            category_obj.deposit(amount, description_from)
            return True
        return False

    def check_funds(self, amount):
        if self.get_balance() >=  amount:
            return True
        return False

def repeat_char(char, n):
    return ''.join(char for i in range(n))

def draw_col_header(level):
    char_to_fill = 3 - (len(str(level)))
    return repeat_char(' ', char_to_fill) + str(level) + '|'

def draw_percentage_line(list_cat_per):
    line_str =''
    result_line = ''

    for level in range(100, -10, -10):
        line_str = draw_col_header(level)
        for i in range(len(list_cat_per)):
            if list_cat_per[i][1] >= level:
                line_str += ' o '
            else:
                line_str += repeat_char(' ', 3)
        
        line_str += repeat_char(' ', 1) + '\n'
        result_line += line_str
    return result_line

def draw_line_seperator (num_underscore):
    line_seperator = repeat_char(' ', 4) + repeat_char('-' , (num_underscore*3) + 1 )
    line_seperator += '\n'
    return line_seperator

def draw_category_line(list_cat_per):
    line_str = ''

    max_cat_len = max([len(list_cat_per[i][0]) for i in range(len(list_cat_per))])
    
    for i in range(max_cat_len):
        line_str += repeat_char(' ', 4)
        for j in range(len(list_cat_per)):
            if i < len(list_cat_per[j][0]):
                line_str += ' ' + list_cat_per[j][0][i] + ' '
            else:
                line_str += repeat_char(' ', 3)
        line_str += repeat_char(' ', 1) + '\n'


    # delete the last new line char
    return line_str[0:-1]

def list_category_percentate(categories):
    list_cat = []
    percentage = 0
    whole = 0
    total = 0
   
    for category in (categories):
        for member in range (len(category.ledger)):
            if category.ledger[member]['description'] == 'deposit':
                whole += category.ledger[member]['amount']
            else: 
                total += (category.ledger[member]['amount'])*-1
        
        if whole == 0:
            percentage = 0
        else: 
            percentage = (int (((100*total)/whole) // 10 )) * 10

        list_cat.append([category.cat, percentage])
        whole = 0
        total = 0

    return list_cat

def create_spend_chart(categories):
    title = 'Percentage spent by category'
    result_str = ''
    list_cat_per = list_category_percentate(categories)

    result_str += draw_percentage_line(list_cat_per)
    line_seperator = draw_line_seperator(len(list_cat_per))
    result_str = title + '\n' + result_str + line_seperator
    result_str += draw_category_line (list_cat_per)
    
    return result_str



### Your code so far


```py

Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

Can you share some more information?

19. The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.

Can you share what percentages you are calculating for a given output?
How would you prove out this section and ensure your percentages are correct?
What tests have you run?

24. create_spend_chart should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details.

Can you share the assertion error that you’re receiving?

food = Category('Food')
food.deposit(1000, '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)

print(create_spend_chart([food,clothing]))

Your output:

Percentage spent by category
100|       
 90|       
 80|       
 70|       
 60|       
 50|       
 40|       
 30|       
 20|       
 10|       
  0| o  o  
    -------
     F  C  
     o  l  
     o  o  
     d  t  
        h  
        i  
        n  
        g  

These should add up to approximately 100%. You need to calculate what percent was spent in each category, from the total spent (not the total deposited)

The percentage spent should be calculated only with withdrawals and not with deposits, and it should be the percentage of the amount spent for each category to the total spent for all categories.

Thank you for pointing out to the correct percentage calculation,
I was calculating percentage of each category in respect of total deposit of that category.
Once I fixed the calculation to be spend of each category to respect of total of all categories, all test are passed :slight_smile:

1 Like