Python Basics: Recursive function return value (return) error

Source: Internet
Author: User

The return statement is used to exit the function and return an expression to the caller. return without parameters (or write a return statement),
Default returns NONE. None is a special value, and its data type is nonetype. Nonetype is a special type of Python, and it has only one value of none.
It does not support any operations or any built-in methods, and any other data type comparison is equal to always return false, or none can be assigned to any variable ...

1 when the function does not have an explicit return, the default value of None is returned

2 "When the recursive function has a return, return in the place of recursion, otherwise it will always return none.

1 Import Time2 defBinary_search (data,find_n):3len=len (data)4     ifLen==1:5         ifdata[0]==Find_n:6             #print ("Found, in%d"% (1))7             return18         Else:9             #print ("Not Found")Ten             return0 One     eliflen==2: A         ifdata[0]==Find_n: -             #print ("Found, in%d"% (1)) -             return1 the         elifdata[1]==Find_n: -             #print ("Found, in%d"% (2)) -             return1 -         Else: +             #print ("Not Found") -             return0 +     Else:         AMid_n=int (LEN/2) atMid_val=Data[mid_n] -         ifmid_val==Find_n: -              #print ("Found, in%d"% (mid_n+1)) -              return1 -         elifmid_val<Find_n: -Right_val=Data[mid_n:] in              returnBinary_search (right_val,find_n) -         Else: toLeft_val=Data[:mid_n] +              returnBinary_search (left_val,find_n) -  the defsplit_data (n): *start=0 $stop=1000Panax NotoginsengFlag=1 -len=N the      whileflag: +         iflen>1000: AData=[x forXinchRange (start,stop,1)] the             yieldData +start=start+1000 -stop=stop+1000 $len=len-1000 $         Else: -Data=[x forXinchRange (start,n,1)] -             yieldData theflag=0 -         Wuyi          the if __name__=="__main__": -t0=time.time () Wu      forDatainchSplit_data (10000000000000): -F=binary_search (data,999999999) About         #print (f) $         ifF: -             Print("have been found") -              Break -T1=time.time ()-T0 A     Print(t1)
View Code

Elif Mid_val<find_n:
Right_val=data[mid_n:]
return binary_search (right_val,find_n)

Else
Left_val=data[:mid_n]
return binary_search (left_val,find_n)

Python Basics: Recursive function return value (return) error

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.