Build a Budget App - Build a Budget App

Tell us what’s happening:

I’m failing 20 , 23 and 24 and I don’t know exactly why. F12 console only tells me there is an assertion error.

Your code so far

class Category:
    def __init__(self,name):
        self.name=name
        self.ledger = []
        self.stored_amount = 0
    
    def deposit(self,amount,description=""):
        if description:
         description = description
        else:
         description=""
        self.ledger.append({'amount': amount, 'description': description})
        self.stored_amount += amount
        #import pdb ; pdb.set_trace()
    
    def __eq__(self,other):
        if isinstance(other,Category):
            return self.name == other.name

    def withdraw(self,amount,description = ""):
        #import pdb ; pdb.set_trace()
        if  self.check_funds(amount) == False:
            return False
        else:
            amount = -amount
            self.ledger.append({'amount': amount, 'description': description})
            self.stored_amount += amount
            return True
    
    def get_balance(self):
        return self.stored_amount 
    
    def transfer(self,amount,category):
        if  not isinstance(category,Category):
            raise TypeError('El parametro para transfer debe ser de la clase CATEGORY, intenta de nuevo')
        if self.check_funds(amount) == False:
            return False
        else:
            self.withdraw(amount,f'Transfer to {category.name}')
            category.deposit(amount,f'Transfer from {self.name}')

            return True
            
    
    def check_funds(self,amount):
        if amount > self.stored_amount:
            return False
        else:
            return True
    
    def __str__(self):
        displayed_text = str(self.name).center(30,'*') + '\n'
        #displayed_text += "\nInitial "
        for dictio in self.ledger:
            displayed_text +=str(dictio["description"])[0:23].ljust(23) + str(format(dictio["amount"],'.2f'))[0:7].rjust(7," ") + '\n'
        displayed_text += f'Total: {self.get_balance()}'
        return str(displayed_text)



def create_spend_chart(categories):
    display_text = 'Percentage spent by category'
    display_text += '\n'  
    spent_sum = sum(value['amount'] for category in categories for value in category.ledger if value['amount'] < 0)
    spent_sum = -spent_sum
    #import pdb ; pdb.set_trace()
    percentages = []
    for category in categories:
        sum_amount = 0
        for value in category.ledger:
            if value['amount'] < 0:
                sum_amount += -value['amount']
        percentage_spent = int((sum_amount/spent_sum) * 100)
        percentages.append(percentage_spent - (percentage_spent % 10))
    for i in range(100,-10,-10):
        line = str(i).rjust(3) + '|'
        for pct in percentages:
            if i <= pct :
                line += ' o '
            else:
                line += '   '
        line += '  '
        display_text+= line + '\n'
        #print(i,len(line))
    display_text += '    -' + '---' * len(categories) + '\n'
    #import pdb ; pdb.set_trace()
    max_len = max(len(c.name) for c in categories)
    for i in range(max_len):
        line = "     "
        for c in categories:
            if i < len(c.name):
                line += c.name[i] + "  "
            else:
                line += "   "
        line += "  "
        display_text += line + '\n'
        #print(i,len(line))
    return display_text.rstrip('\n')

food = Category('food')
clothing= Category('clothing')
work = Category('work')
auto = Category('auto')

auto.deposit(4000,'deposit')
food.deposit(1000,'deposit')
clothing.deposit(1000,'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
food.withdraw(340,'Algo')
food.withdraw(280,'Algo')
#clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.withdraw(130,'usuario')
clothing.withdraw(130,'otros')
auto.withdraw(240,'carros y mas')
auto.withdraw(1080,'carros y mas')

list1=[auto,food,clothing]
print(create_spend_chart(list1))
#food.withdraw(45.67,'milk, cereal, eggs, bacon, bread')

#clothing.deposit(1500)
#work.deposit(3500,'Initial deposit')

#food.transfer(100,clothing)
#clothing.transfer(243.20,work)

#print(food.get_balance())
#print(clothing.get_balance())
#print(work.get_balance())

#print(food)

#print(food.ledger)
#print(clothing.ledger)

#category1.deposit(100,'')
#category1.withdraw(50,'Budget dool')
#category1.withdraw(120,'great')

#print(category1.get_balance())

#print(product1.ledger.items())















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

Hi @Exikluk

Here is an assertion error.

AssertionError: 
     B  F  E   \n     u  o  n   \n     s  o  t   \n    [160 chars]t   ' != '
      B  F  E  \n     u  o  n  \n     s  o  t  \n     i [147 chars] t  '

Looks like your function has a spacing issue.

I’m not sure which is which. But one line is the expected string, the other is the actual string produced by your function.

Happy coding

1 Like

actual then expected, assertions are by default like that