我正在做一些处理 Python 类的作业。但是我卡在了一个我不太明白的部分。
它告诉我:
--> 编写一个名为withdraw()
的方法,从帐户中提取指定金额。帐户中的余额减少方法参数中指定的金额。仅当参数中指定的金额小于或等于余额时,才应减少余额
这是我的程序
class Account:
def __init__(self,id=0):
self.__id = id
self.__balance = 0
self.__annualInterestRate = 0
def getid(self):
return self.__id
def getbalance(self):
return self.__balance
def getannualInterestRate(self):
return self.__getannualInterestRate
def setid(self):
self.__id = id
def setbalance(self):
self.__balance = balance
def getMonthlyInterestRate(self):
return self.__annualInterestRate/12
def getMonthlyInterest(self):
return self.__balance * getMonthlyInterestRate()
然后我将不得不:
def withdraw():
# I don't know what to do here
最佳答案
您需要传递隐含的 self
参数(此处和其他一些方法)和 amount
:
def withdraw(self, amount):
# subtract amount from self.__balance
在继续上课之前,您还应该阅读 return
语句。
关于python - python中一个类的提款方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15807043/