Scottb0898
Scottb0898

Reputation: 37

Trying to do a simple model of a bank account for school and the function get_balance yields an error

After i run the code through python 3 I get this error

TypeError: 'int' object is not callable

Here is my code...

class Account:
    def __init__(self, number, name, balance):
            self.number = number
            self.name = name
            self.balance = int(balance)

    def get_balance(self):
            return self.balance()

    def deposit(self, amount):
            self.balance += amount
            return self.balance

    def withdraw(self, amount):
            self.balance -= amount
            return self.balance
def test():
    print('Accounts:')
    a1 = Account(123, 'Scott', 10)
    a1 = Account(124, 'Donald T.', 1000000)

    print('Testing get_balance:')
    print(a1.get_balance())
    print(a2.get_balance())

    print('Testing deposit:')
    a1.deposit(100)
    a2.deposit(50)
    print(a1.get_balance())
    print(a2.get_balance())

    print('Testing withdraw:')
    a1.withdraw(100)
    a2.withdraw(50)
    print(a1.get_balance())
    print(a2.get_balance())

I run python and i enter this...

>>>import bank
>>>bank.test()

Afterwords, this is printed

Accounts:
Testing get_balance:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/scottb0898/CSE1010/HW5/bank.py", line 23, in test
    print(a1.get_balance())
  File "/home/scottb0898/CSE1010/HW5/bank.py", line 9, in get_balance
    return self.balance()
TypeError: 'int' object is not callable
>>>

I'm sure it's a very simple fix, but I just can't figure out what's wrong. Thanks for the help!

Upvotes: 1

Views: 317

Answers (4)

brunormoreira
brunormoreira

Reputation: 248

Change these lines:

def get_balance(self):
        return self.balance #remove '()'

and

a2 = Account(124, 'Donald T.', 1000000) #a2 instead a1

Upvotes: 1

Anonymous1847
Anonymous1847

Reputation: 2598

Just replace line 9 with:

return self.balance

self.balance is a number, not a function, thus generating an error when you try to call it.

Upvotes: 1

Mayank Mittal
Mayank Mittal

Reputation: 75

there are mistakes in your code. In get_balance() function you are returning self.balance() where there should be self.balance, only. Also, you are creating two objects inside test of same name 'a1'.

Upvotes: 0

jath03
jath03

Reputation: 2359

self.balance is only a variable, not a function. It doesn't need to be called, just change get_balance to

def get_balance(self):
    return self.balance

Upvotes: 2

Related Questions