';int';对象在python中不可调用
我得到了这个,当我打印x.draw()时,我希望它打印410 凯尔12345 500 回溯(最近一次呼叫最后一次): 文件“bank.py”,第21行,在 打印x.撤消() TypeError:“int”对象不可调用 这是我的密码:';int';对象在python中不可调用,python,class,methods,Python,Class,Methods,我得到了这个,当我打印x.draw()时,我希望它打印410 凯尔12345 500 回溯(最近一次呼叫最后一次): 文件“bank.py”,第21行,在 打印x.撤消() TypeError:“int”对象不可调用 这是我的密码: class Bank: def __init__(self, name, id, balance, withdraw): self.name = name self.id = id self.balance
class Bank:
def __init__(self, name, id, balance, withdraw):
self.name = name
self.id = id
self.balance = balance
self.withdraw = withdraw
def print_info(self):
return "%s %d %d" % (self.name, self.id, self.balance)
def withdraw(self):
if self.withdraw > self.balance:
return "ERROR: Not enough funds for this transfer"
elif self.withdraw < self.balance and self.withdraw >= 0:
self.balance = self.balace - self.withdraw
return self.balance
else:
return "Not a legitimate amount of funds"
x = Bank("Kyle", 12345, 500, 90)
print x.print_info()
print x.withdraw()
class银行:
定义初始(自我、姓名、id、余额、取款):
self.name=名称
self.id=id
自我平衡
撤回
def打印信息(自身):
返回“%s%d%d%”(self.name、self.id、self.balance)
def提取(自身):
如果self.draw>self.balance:
return“错误:没有足够的资金进行此转账”
elif self.draw=0:
self.balance=self.balace-self.draw
回归自我平衡
其他:
返回“非合法金额的资金”
x=银行(“凯尔”,1234550090)
打印x.打印信息()
打印x.撤消()
我是否需要修复类本身中的某些问题,或者我的方法调用有问题?您在实例上设置了一个同名属性:
self.withdraw = withdraw
它是您现在试图调用的属性,而不是方法。Python不区分方法和属性,它们不存在于单独的名称空间中
为属性使用不同的名称<代码>撤回(撤回的过去时)作为一个更好的属性名称出现在脑海中:
class Bank:
def __init__(self, name, id, balance, withdrawn):
self.name = name
self.id = id
self.balance = balance
self.withdrawn = withdrawn
def print_info(self):
return "%s %d %d" % (self.name, self.id, self.balance)
def withdraw(self):
if self.withdrawn > self.balance:
return "ERROR: Not enough funds for this transfer"
elif self.withdrawn < self.balance and self.withdrawn >= 0:
self.balance = self.balance - self.withdrawn
return self.balance
else:
return "Not a legitimate amount of funds"
class银行:
定义初始(自我、姓名、id、余额、已提取):
self.name=名称
self.id=id
自我平衡
自我撤回
def打印信息(自身):
返回“%s%d%d%”(self.name、self.id、self.balance)
def提取(自身):
如果自动提取>自动平衡:
return“错误:没有足够的资金进行此转账”
elif自收回<自平衡和自收回>=0:
self.balance=self.balance-self.draked
回归自我平衡
其他:
返回“非合法金额的资金”
(我还更正了一个输入错误;您在一个位置使用了
balance
,您打算在该位置使用balance
)。您在实例上设置了一个同名属性:
self.withdraw = withdraw
它是您现在试图调用的属性,而不是方法。Python不区分方法和属性,它们不存在于单独的名称空间中
为属性使用不同的名称<代码>撤回(撤回的过去时)作为一个更好的属性名称出现在脑海中:
class Bank:
def __init__(self, name, id, balance, withdrawn):
self.name = name
self.id = id
self.balance = balance
self.withdrawn = withdrawn
def print_info(self):
return "%s %d %d" % (self.name, self.id, self.balance)
def withdraw(self):
if self.withdrawn > self.balance:
return "ERROR: Not enough funds for this transfer"
elif self.withdrawn < self.balance and self.withdrawn >= 0:
self.balance = self.balance - self.withdrawn
return self.balance
else:
return "Not a legitimate amount of funds"
class银行:
定义初始(自我、姓名、id、余额、已提取):
self.name=名称
self.id=id
自我平衡
自我撤回
def打印信息(自身):
返回“%s%d%d%”(self.name、self.id、self.balance)
def提取(自身):
如果自动提取>自动平衡:
return“错误:没有足够的资金进行此转账”
elif自收回<自平衡和自收回>=0:
self.balance=self.balance-self.draked
回归自我平衡
其他:
返回“非合法金额的资金”
(我还更正了一个输入错误;您在一个您打算使用余额的位置使用了余额
)