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',因此重啓。 –