python - TypeError: unsupported operand type(s) for -: 'float' and 'method' -
so guys, i'm trying work exercises in python after reading classes , objects, , 1 of said exercises create account class , write methods withdraw , deposit money account. every time run it, typeerror telling me operand unsupported floats , methods. feel i'm close missing obvious. give me idea i'm doing wrong , how 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 " + str(account.getid())) print("balance " + str(account.getbalance())) print("monthly interest rate is" + str(account.getmonthlyinterestrate())) print("monthly interest " + str(account.getmonthlyinterest())) main()
this:
def withdraw(account, balance): return (account.__balance) - (account.withdraw) should this:
def withdraw(self, amount): self.__balance -= amount in python refer class inside methods self.
applying rest of class , cleaning 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
Comments
Post a Comment