"Python" two-point lookup + receive no return value problem grooming

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.