TypeError: unsupported operand type(s) for -: 'float' and 'method'
TypeError: unsupported operand type(s) for -: 'float' and 'method'
伙计们,在阅读了 类 和对象之后,我正在尝试在 Python 中做一些练习,其中一个练习是创建一个帐户 Class 并编写将钱存入帐户的方法。每次我 运行 它时,我都会收到一个 TypeError 告诉我该操作数不支持 Floats 和 Methods。我觉得我很接近但错过了一些非常明显的东西。谁能告诉我我做错了什么以及如何解决这个问题?
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()
这个:
def withdraw(account, balance):
return (account.__balance) - (account.withdraw)
应该看起来像这样:
def withdraw(self, amount):
self.__balance -= amount
在 Python 中,我们总是将 class 内部方法称为 self
。
将此应用到 class 的其余部分并清理一些东西:
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
伙计们,在阅读了 类 和对象之后,我正在尝试在 Python 中做一些练习,其中一个练习是创建一个帐户 Class 并编写将钱存入帐户的方法。每次我 运行 它时,我都会收到一个 TypeError 告诉我该操作数不支持 Floats 和 Methods。我觉得我很接近但错过了一些非常明显的东西。谁能告诉我我做错了什么以及如何解决这个问题?
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()
这个:
def withdraw(account, balance):
return (account.__balance) - (account.withdraw)
应该看起来像这样:
def withdraw(self, amount):
self.__balance -= amount
在 Python 中,我们总是将 class 内部方法称为 self
。
将此应用到 class 的其余部分并清理一些东西:
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