recursive function return value problems

bonono at gmail.com bonono at gmail.com
Thu Dec 29 00:13:54 EST 2005


randomtalk at gmail.com wrote:
> 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?
seems that you want the reTest(True) to be the last and skip the rest.
If that is the case, put it as "return reTest(True)". Otherwise, the
last three statements(print, print, return) would happen regardless the
boolean value.




More information about the Python-list mailing list