我正在尝试这样做:
def enter():
print("hi")
enter()
但是,起初它给了我:
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
hi
但是,在数据流之后,它开始给我这样的错误:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
enter()
File "C:/Users/offcampus/AppData/Local/Programs/Python/Python35/len finder.py", line 11, in enter
它说我已经达到最大深度的递归错误,有人可以告诉我如何解决。
如果有人告诉我递归错误甚至意味着什么,也将不胜感激。
提前谢谢!
答案 0 :(得分:2)
在内部调用函数称为递归,它是循环的替代方法。但这是有代价的。 Python需要“记住”以前对该函数的所有调用,而不是每次都会重置的循环。因此,您可以理解为什么您的计算机最终会耗尽内存,因为您没有 stop 调用Dim cell As Range
Dim rng As Range
Dim Position As Integer
Set rng = ThisWorkbook.Worksheets("Data").Range("A1:A4000")
For Each cell In rng
Position = InStr(cell.Value, "(") - 2
cell.Value = CStr(Left(cell.Value, Position))
Next cell
。
这称为堆栈溢出-即,当计算机的“堆栈”(内存)溢出(变得太高且有点倒塌)时。其他较旧的语言(例如C)根本无法很好地解决这一问题,并且堆栈溢出有点儿成为程序员要处理的事情(这就是为什么将其称为StackOverflow.com的原因-帮助其他问题)。
为避免这种情况,Python设置了可以递归的距离限制,而超过此限制将引发错误(而不是破坏计算机)。为防止您的代码达到此限制(非常高),您需要指定一个退出条件。在这里您返回,而不是再次递归。这可以像if语句一样简单,也可以更复杂。这也是有道理的。您现在拥有它的方式,很明显为什么它会不断递归,您永远不会告诉它不这样做。