Recursive
Continue running functions within the function, knowing that a goal is reached and the final return value.
Characteristics of recursion:
1. The function must have a definite end condition
2. Without entering a recursion, the problem that needs to be addressed should be reduced
3. Recursive efficiency is low, need to prevent memory overflow
Class Exercises
1. Ask the way
#Ask the way, there are several people on the road, ask if one of them knows where the place is.s1=['Alex','Xuzheng','Xiaoye','Haibin']defWenlu (S1): person=s1.pop (0)#the list of passers-by is paged out for questioning, and the list of non-questions is reduced ifperson = ='Haibin':#only the seashore knows the way. return 'Ah, I know how to get to the Taiping container .' ifLen (S1) = =0:return 'no one knows where the road is.' Print('I'm sorry, I don't know, I'll ask you.') Res=Wenlu (S1)returnResres=Wenlu (S1)Print(RES)
Very coarse recursion.
str = input (' Please enter several characters:')def F (x): if x = =-1: return "' Else : return Str[x] + f (x-1)print(f (Len (str)-1)----Alex= = = Xela
Wrote it myself.
def Zhuan (x): if x = = 0 :return' else: a=s[x-1]+zhuan (x-1 ) return ab=Zhuan (len (s))print(b)
2018-6-6-python full stack development day15-part2-recursion