return 语句在 python 递归中不返回任何内容 [英] return statement doesnt return anything in python recursion
本文介绍了return 语句在 python 递归中不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的方法在字符串中查找是否有任何 python 方法.
def there_is_a_call( string ):返回 string.find('(') > -1def find_and_remove_functions( string , found_functions ):如果没有 there_is_a_call( string ):打印(找到函数)返回 found_functions别的:function_end = string.find('(')function_string = string[:function_end][::-1]if function_string.find('.') >-1 :index = function_string.find('.')elif function_string.find(' ') >-1:index = function_string.find(' ')别的:索引 = len(function_string) - 1func_name = function_string[ : index + 1 ][::-1] + '()'new_list = found_functionsnew_list.append(func_name)find_and_remove_functions( string[ function_end + 1: ], found_functions )
所以我尝试看看它是否有效,然后发生了这种情况;
<预><代码>>>>>a = find_and_remove_functions( 'func() and some more()' , [] )['func()', '更多()']>>>>打印(一)没有任何为什么在打印 found_functions
时 return 语句不返回任何内容?
解决方案
这里:
find_and_remove_functions( string[ function_end + 1: ], found_functions )
应该
return find_and_remove_functions( string[ function_end + 1: ], found_functions )
The methods below look in a string to find if it has any python methods.
def there_is_a_call( string ):
return string.find('(') > -1
def find_and_remove_functions( string , found_functions ):
if not there_is_a_call( string ):
print( found_functions )
return found_functions
else:
function_end = string.find('(')
function_string = string[:function_end][::-1]
if function_string.find('.') > -1 :
index = function_string.find('.')
elif function_string.find(' ') > -1:
index = function_string.find(' ')
else:
index = len(function_string) - 1
func_name = function_string[ : index + 1 ][::-1] + '()'
new_list = found_functions
new_list.append( func_name )
find_and_remove_functions( string[ function_end + 1: ], found_functions )
So I try to see if it works and then this happens;
>>>> a = find_and_remove_functions( 'func() and some more()' , [] )
['func()', ' more()']
>>>> print(a)
None
Why is the return statement not returning anything while the found_functions
do get printed?
解决方案
Here:
find_and_remove_functions( string[ function_end + 1: ], found_functions )
should be
return find_and_remove_functions( string[ function_end + 1: ], found_functions )
这篇关于return 语句在 python 递归中不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文