jbar0787
jbar0787

Reputation: 3

TypeError: unsupported operand type(s) for -: 'float' and 'method'

So guys, I'm trying to work some exercises in Python after reading about classes and objects, and one of said exercises is to create an Account Class and write methods to withdraw and deposit money into an account. Every time I run it, I get a TypeError telling me that the operand is unsupported for Floats and Methods. I feel like I'm close but missing something really obvious. Could anyone give me an idea as to what I'm doing wrong and how to fix this?

class Account:
    def __init__(account, id, balance, rate):
        account.__id = id
        account.__balance = float(balance)
        account.__annualInterestRate = float(rate)

    def getMonthlyInterestRate(account):
        return (account.__annualInterestRate/12)
    def getMonthlyInterest(account):
        return account.__balance*(account.__annualInterestRate/12)   
    def getId(account):
        return account.__id
    def getBalance(account):
        return account.__balance
    def withdraw(account, balance):
        return (account.__balance) - (account.withdraw)
    def deposit(account, balance):
        return (account.__balance) + (account.deposit)

def main():
    account = Account(1122, 20000, 4.5)
    account.withdraw(2500)
    account.deposit(3000)
    print("ID is " + str(account.getId()))
    print("Balance is " + str(account.getBalance()))
    print("Monthly interest rate is" + str(account.getMonthlyInterestRate()))
    print("Monthly interest is " + str(account.getMonthlyInterest()))

main()

Upvotes: 0

Views: 4962

Answers (1)

orlp
orlp

Reputation: 117701

This:

def withdraw(account, balance):
    return (account.__balance) - (account.withdraw)

Should look something like this:

def withdraw(self, amount):
    self.__balance -= amount

In Python we always refer to the class inside methods as self.

Applying this to the rest of the class and cleaning some things up:

class Account:
    def __init__(self, id, balance, rate):
        self.id = id
        self.balance = float(balance)
        self.annualInterestRate = float(rate)

    def getMonthlyInterestRate(self):
        return self.annualInterestRate / 12

    def getMonthlyInterest(self):
        return self.balance * self.getMonthlyInterestRate()

    def getId(self):
        return self.id

    def getBalance(self):
        return self.balance

    def withdraw(self, amount):
        self.balance -= amount

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

Upvotes: 2

Related Questions