found = 0
def new(string):
global found
if found > len(string):
return 0
fish = string.find('x',found,len(string))
found = fish + 1
return new(string) + 1
text = 'onxonxoinxoinoxn'
final_text = text + 'x'
print new(final_text)
所以我是新的递归,我知道有一个更简单的方法来做到这一点,但有人可以解释如何解决this.this基本上是一个递归函数来找到总次数在变量'text'中可以找到一个字母'x'。递归Python脚本
This is my error:
4
7
11
16
18
0
4
7
Traceback (most recent call last):
11
16
File "/Users/Charana/Documents/Projects/untitled/Main.py", line 18,
在 新(final_text)
RuntimeError:最大递归深度超过
所以它的工作原理,但它仍然loop.how做我让它停下 三江源提前
您应该测试'fish'不'-1'因为这意味着没有'x'任何更多的(退出)。在你的情况下,'found'获得'fish + 1',这是'0',因此重启。 –