One or two points find
1 defErfen_search (para1,find_num):2 " "3 Two-point search4 :p Aram Para1: A range to find, such as a list or a tuple5 :p Aram Find_num: The number to find6 : return:7 " "8Mid_index = Len (para1)//29Mid_num =Para1[mid_index]Ten Try: One ifFind_num = =Mid_num: A Print('got it.') - elifFind_num >Mid_num: -PARA1 = para1[mid_index+1:] the Erfen_search (para1,find_num) - elifFind_num <Mid_num: -PARA1 =Para1[:mid_index] - Erfen_search (para1,find_num) + exceptIndexerror: - Print('not found') + A #function Call atls = [1,2,3,30,44,56,789,9000] -Erfen_search (ls,56)
Second, thinking and questions:
1. In the above two points to find the code based on the optimization again, want to find a successful, receive a return value, such as the return value of the received content is true
1 defErfen_search (para1,find_num):2 " "3 Two-point search4 :p Aram Para1: A range to find, such as a list or a tuple5 :p Aram Find_num: The number to find6 : return:7 " "8Mid_index = Len (para1)//29Mid_num =Para1[mid_index]Ten Try: One ifFind_num = =Mid_num: A Print('got it.') - returnTrue#added a line of code to receive the return value - elifFind_num >Mid_num: thePARA1 = para1[mid_index+1:] - Erfen_search (para1,find_num) - elifFind_num <Mid_num: -PARA1 =Para1[:mid_index] + Erfen_search (para1,find_num) - exceptIndexerror: + Print('not found') A #function Call atls = [1,2,3,30,44,56,789,9000] -Resault = Erfen_search (ls,56)#receive return values with Resault - Print(Resault)#打印之后发现, the value received by Resault is None??? Why??? - - #Execution Results - got it. in None
2. Self-debugging one will find that the binary lookup uses the recursive function, the first layer of recursion, and then back to the layer of return, then the last function to receive the returned value is actually in the last back to the function of the return value of whichever, so I did a simple comb and analysis. Know why the last data query succeeded, but there is no return value;
"Python" two-point lookup + receive no return value problem grooming