Build a Budget App - Build a Budget App

Tell us what’s happening:

Can someone help me as to why my deposit function isn’t working? I think it is creating an object in the ledger but the console says otherwise.

Also, can someone tell me why balance is unbound when I check it at check_funds? I get an error saying cannot access local variable ‘balance’ where it is not associated with a value

Your code so far

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

    def deposit(self, amount, description = ""):
        self.balance += amount
        self.ledger.append({'amount': amount, 'description': description})
    
    def withdraw(self, amount, description = ""):
        if self.check_funds(amount):
            balance -= amount
            self.ledger.append({'amount': amount, 'description': description})
            return True
        else:
            return False
    
    def check_funds(self, amount):
        if (amount > self.balance):
            return False
        else:
            return True

def create_spend_chart(categories):
    pass

food = Category("Food")
food.withdraw(-5)
print(food.ledger)

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

Challenge Information:

Build a Budget App - Build a Budget App

Hi @niladrideb ,

Traceback (most recent call last):
  File "main.py", line 29, in <module>
  File "main.py", line 13, in withdraw
UnboundLocalError: cannot access local variable 'balance' where it is not associated with a value

Does your class have an instance variable named balance?

A withdraw method that accepts an amount and an optional description (default to an empty string). The method should store in ledger the amount passed in as a negative number

The withdrawal should be negated in the method, not the function call.

Why do you think your deposit method isn’t working? How have you tested it?

Happy coding!

Oh I see. I have changed that and changed balance -= amount to self.balance -= amount

Now I gotta ask this - the withdraw method should create a specific object in the ledger when called and should make an entry with blank description when it is not provided. I think my code does that but why does it not satisfy these conditions? Is there anywhere I am going blind while writing the code?

What if you don’t have enough money to make a withdrawal?