Python 在类中的函数内重复函数
我是一个初学者,我如何解决我的问题,我想重复另一个函数之前,它被调用 这是我的代码:Python 在类中的函数内重复函数,python,Python,我是一个初学者,我如何解决我的问题,我想重复另一个函数之前,它被调用 这是我的代码: class Banking: def __init__ (self): self.balance = 0 def intro (self): print ('Welcome to Banking and services') def deposit (self): dep = float (input ('Enter amount to
class Banking:
def __init__ (self):
self.balance = 0
def intro (self):
print ('Welcome to Banking and services')
def deposit (self):
dep = float (input ('Enter amount to be deposited: '))
self.balance += dep
def withdraw (self):
wthdrw = float (input ('Enter amount to b withdrewn: '))
if wthdrw > self.balance:
print ('Insufficient balance')
else:
self.balance -= wthdrw
print ('You have the remaining,', self.balance, "Net Amount Balance")
def again (self):
while True:
agn = input ('New transaction? (yes/no)').lower().strip()
if agn == 'no':
print ('Ok, bye have a great day')
break
elif agn == "yes":
intro (self)
deposit (self)
withdraw (self)
again (self)
bank = Banking()
bank.intro()
bank.deposit()
bank.withdraw()
bank.again()
当我运行代码时,从intro到Draw的输出是正常的,但当再次出现时,它会说:
Traceback (most recent call last):
File "tnp_10ten.py", line 36, in <module>
bank.again()
File "tnp_10ten.py", line 27, in again
intro (self)
NameError: name 'intro' is not defined
回溯(最近一次呼叫最后一次):
文件“tnp_10ten.py”,第36行,在
银行再次()
再次输入第27行的文件“tnp_10ten.py”
简介(自我)
名称错误:未定义名称“简介”
这里有一个修复功能:
再次定义(自我):
尽管如此:
agn=输入('新事务?(是/否)).lower().strip()
如果agn==‘否’:
打印('好的,再见,祝您愉快')
返回
elif agn==“是”:
self.intro()
自付定金()
self.draw()
正如其他人所解释的,您需要使用self.func()来引用当前的class函数。
此外,如果用户插入“否”且未中断,则需要返回。如果中断,则需要插入与您之前说过“是”的次数相同的“否”。当从同一类调用函数时,您需要使用
self。
因此,在函数中,您需要再次更改
intro (self)
deposit (self)
withdraw (self)
again (self)
到
在python中,应该通过self
引用类方法。因此,将您的代码从intro(self)
更改为self.intro()
。另外,不是您要问的bug,而是一个bug:removeReach
调用Reach函数,已经有一个while True
循环,这里不需要递归。实际上,从Reach
函数中删除self.Reach()
,这里已经有一个,而True
,不需要递归。
self.intro()
self.deposit()
self.withdraw()