递归函数返回值问题 [英] recursive function return value problems
问题描述
我有以下递归函数(简化演示
问题):
def reTest(bool):
....结果= []
....如果不是bool:
... .reTest(真)
....其他:
....打印YAHHH"
....结果= [应该是唯一退回的东西]]
....打印打印结果:
....打印结果
....返回结果
.... reTest(False)
YAHHH
打印结果:
[''应该是唯一退回的''']
打印结果:
[]
[]
我不明白为什么结果会被退回两次?有什么东西
特别我错过了递归函数吗?
ra ******** @ gmail.com 写道:def reTest(bool):
... result = []
...如果不是bool:
... reTest(True)
...其他:
...打印YAHHH
...结果= ["应该是唯一返回的内容]
...打印打印结果:"
...打印结果
...返回结果
...
reTest(False)
你' '既打印结果又返回结果。解释器只是
恰好显示打印和返回的变量。
显示返回变量的示例:
def returnb ():
....返回''b''
.... returnb()
''b''
希望有所帮助,
Brian
-
如果宇宙是一个chrooted环境,所有东西
从主机符号链接怎么办?
2005年12月28日15:25: 30 -0800,
ra********@gmail.com 写道:
我有以下递归函数(简化来演示问题):
def reTest(bool):
... result = []
...如果不是bool:
... reTest(True)
不要这样做。请改为:
result = reTest(True)
[其余示例剪断]
HTH,
Dan
-
Dan Sommers
< http:/ /www.tombstonezero.net/dan/>
最终返回的值是:[]
打印的两个值是(注意我只有一个打印声明
打印打印结果,但是,在实际情况下,它打印了两次b $ b):
打印结果:
[''应该是唯一返回的'']
打印结果:
[] <很遗憾,我不认为你理解我的问题
正确..
hi, i have the following recursive function (simplified to demonstrate
the problem):
def reTest(bool): .... result = []
.... if not bool:
.... reTest(True)
.... else:
.... print "YAHHH"
.... result = ["should be the only thing returned"]
.... print "printing result: "
.... print result
.... return result
.... reTest(False)
YAHHH
printing result:
[''should be the only thing returned'']
printing result:
[]
[]
I don''t understand why results are returned twice? is there something
special i missed about recursive functions?
ra********@gmail.com wrote:def reTest(bool):
... result = []
... if not bool:
... reTest(True)
... else:
... print "YAHHH"
... result = ["should be the only thing returned"]
... print "printing result: "
... print result
... return result
...
reTest(False)
You''re both printing the result and returning it. The interpreter just
happens to show both printed and returned variables.
An example of a shown return variable:
def returnb(): .... return ''b''
....returnb()
''b''
Hope that helps,
Brian
--
What if the Universe were a chrooted environment with everything
symlinked from the host?
On 28 Dec 2005 15:25:30 -0800,
ra********@gmail.com wrote:
hi, i have the following recursive function (simplified to demonstrate
the problem):
def reTest(bool):
... result = []
... if not bool:
... reTest(True)
Don''t do that. Do this instead:
result = reTest(True)
[ rest of example snipped ]
HTH,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
the final returned value is: []
the two values printed is (note i only have one print statement
printing "print result",. however, in the actualality, it''s printed
twice):
printing result:
[''should be the only thing returned'']
printing result:
[]
therefore, sadly, i don''t thinkg you''ve understand my problem
correctly..
这篇关于递归函数返回值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!