Build a Budget App - Build a Budget App Test 19 and 24

Tell us what’s happening:

Hi,

Could someone please point me in the right direction? Can’t seem to pass test 19 and 24. Appreciate it!

Your code so far

from math import floor

class Category:
    def __init__(self, category):
        self.category=category
        self.ledger=[]
        self.budget=0
        withdraw=False
    
    def deposit(self,amount,description=''):
        newEntry={"amount": amount, "description": description}
        self.budget+=amount
        self.ledger.append(newEntry)
    
    def withdraw(self, amount, description=""):
        if self.check_funds(amount):
            self.deposit(-amount,description)
            return True
        else:
            return False
    
    def get_balance(self):
        return self.budget

    def check_funds(self, amount):
        return self.get_balance()>=amount
    
    def transfer(self, amount, to_category):
        if self.check_funds(amount):
            self.budget-=amount
            self.ledger.append({"amount": -amount, "description": f'Transfer to {to_category.category}'})
            to_category.deposit(amount, f'Transfer from {self.category}')
            return True
        else:
            return False
    
    def __str__(self):
        output=''
        title = self.category.center(30,'*')
        output+=title+'\n'

        for item in self.ledger:
            description = item['description']
            amount = item['amount']
            output += f'{description[:23]:<23}{amount:>7.2f}\n'
        output+=f"Total: "
        output+=format(self.budget, '.2f')
        return output

def create_spend_chart(categories):
    output=''
    title = 'Percentage spent by category\n'
    output+=title
    percentage=[]
    spacing=4*' '

    for cat in categories:
        percent=0
        for item in cat.ledger:
            if item['amount']>0:
                percent+=item['amount']
        percent=(percent-cat.budget)*100/percent
        percentage.append(int(percent))

    for i in range(10,-1,-1):
        row=''
        output+=f'{i*10:3}|'
        for p in percentage:
            row+=f' 0 ' if round(p/10)>=i else '   '
        print(len(row)+2)
        output+=row+' \n'
    
    output+=spacing +(len(row)+1)*'-'

    max_length= max(len(str(word.category)) for word in categories)
    
    for i in range(max_length):
        row=''
        for category in categories:
            word= str(category.category)
            row+= " "+word[i]+" " if i<len(str(word)) else "   "
        output+="\n"+ spacing + row +" "
        
    return output


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

food.deposit(1000, "deposit")
food.withdraw(600, "groceries")
food.withdraw(165.60, "books")
food.withdraw(234.10, "books")

clothing.deposit(1000, "deposit")
clothing.withdraw(200, "groceries")

auto.deposit(1000, "deposit")
auto.withdraw(100, "groceries")

print(create_spend_chart([food,clothing,auto]))

print(food)
print(clothing)
print(auto)

Your browser information:

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

Challenge Information:

Build a Budget App - Build a Budget App

Welcome to the forum @2Dcosmatu !

Did you check your browser’s console?

'Perc[77 chars]|          \n 60|          \n 50|          \n [297 chars] t  ' !=
'Perc[77 chars]|    o     \n 60|    o     \n 50|    o     \n [297 chars] t  '

Happy coding!

1 Like

Thank you @dhess . yes i have checked it and i see that it is expecting something else than what my code produces.

It seems as if the percentage calculation which probably throws the error of test 19 is what also causes the test 24 to fail… need some help with that i guess.

You should have a function outside the Category class named create_spend_chart(categories) that returns a bar-chart string. To build the chart:

Start with the title Percentage spent by category.

Calculate percentages from withdrawals only and not from deposits. The percentage should be the percentage of the amount spent for each category to the total spent for all categories (rounded down to the nearest 10).

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

are you calculating the percentages using only withdrawals?

here you are using this, but if the amount is positive, does not it mean that it is a deposit?

yes…

gave it another try:

def create_spend_chart(categories):
    output='Percentage spent by category\n'
    percentage=[]
    spacing=4*' '
    total_budget =0
    
    for cat in categories:
        for item in cat.ledger:
            if item['amount']>0:
                total_budget+=item['amount']
    print (f"SpendingBudget = {total_budget}") 

    for cat in categories:
        spending=0
        for item in cat.ledger:
            
            if item['amount']<0:
                spending+=item['amount']
        print(f"Spending/Category = {spending}")
        percent=-int(spending/total_budget*100//10*10)
        
        percentage.append(percent)

    for i in range(100,-1,-10):
        row=''
        row+=f'{i:3}|'
        for p in percentage:
            row+=f' o ' if p>=i else '   '
        #print(len(row))
        output+=row+' \n'
    
    output+=spacing+(len(row)-3)*'-'

    max_length= max(len(str(word.category)) for word in categories)
    
    for i in range(max_length):
        row=''
        for category in categories:
            word= str(category.category)
            row+= " "+word[i]+" " if i<len(str(word)) else "   "
        output+="\n"+ spacing + row +" "
        
    return output

can you please post again your code?

When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

indeed the main problem was the calculation of the withdrawls. it needed a sum of all withdrawls across all categories, not a total budget.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.