一、 什么是递归函数
如果一个函数在内部调用自身本身,这个函数就是递归函数。
在递归函数中,是一个有去有回的过程。
二、递归函数的说明
def fact(n): if n == 1: return 1 return n * fact(n - 1) print(fact(5)) # fact(5) # ===> 5 * fact(4) # ===> 5 * (4 * fact(3)) # ===> 5 * (4 * (3 * fact(2))) # ===> 5 * (4 * (3 * (2 * fact(1)))) # ===> 5 * (4 * (3 * (2 * 1))) # ===> 5 * (4 * (3 * 2)) # ===> 5 * (4 * 6) # ===> 5 * 24 # ===> 120
通过上面的实例,可以总结出:
递归函数就是在函数内部调用自身本身,直到自身返回的是一个具体的值而不是函数,然后从内向外逐一返回函数的值,因此递归是一个有去有回两个过程组成的;
三、一个递归函数的实例
menu = { '陕西省': { '西安':{ '未央区':{}, '莲湖区':{}, '高新区':{}, }, '咸阳':{ '秦都区':{}, '渭城区':{} } }, '四川省': { '成都':{ '锦江区':{}, '青羊区':{}, '金牛区':{}, }, '绵阳':{ '涪城区': {}, '游仙区': {}, } } } def threeTL(dic): while True: for key in dic: print(key) choice = input('>>>').strip() if choice == 'b' or choice == 'q': return choice elif choice in dic and dic[choice]: res = threeTL(dic[choice]) if res == 'q': return 'q' elif not choice or choice not in dic: continue threeTL(menu)
该实例是选择省打印城市,输入城市打印区, 当用户输入 b 返回上一层,当用户输入 q 退出整个程序,这里最难理解的就是 b 和 q 的用法;
分析如下:
(1)正常递归深入过程:
# def threeTL(dic): # while True: # for key in dic: print(key) # 陕西省、四川省 # choice = input('>>>').strip() # 用户输入:陕西省 # if choice == 'b' or choice == 'q': return choice # elif choice in dic and dic[choice]: # res = threeTL(dic[choice]) # 递归返回 threeTL(dic['陕西省']) # if res == 'q': return 'q' # elif not choice or choice not in dic: # continue # # def threeTL(dic): # dic = dic['陕西省'] # while True: # for key in dic: print(key) # 西安、咸阳 # choice = input('>>>').strip() # 用户输入:西安 # if choice == 'b' or choice == 'q': return choice # elif choice in dic and dic[choice]: # res = threeTL(dic[choice]) # 递归返回 threeTL(dic['陕西省']['西安']) # if res == 'q': return 'q' # elif not choice or choice not in dic: # continue # # def threeTL(dic): # dic = dic['陕西省']['西安'] # while True: # for key in dic: print(key) # 未央莲湖高新 # choice = input('>>>').strip() # # 用户输入:未央区 # if choice == 'b' or choice == 'q': return choice # elif choice in dic and dic[choice]: # res = threeTL(dic[choice]) # if res == 'q': return 'q' # elif not choice or choice not in dic: # 因为未央区没有值,进入下一次循环
threeTL(menu) -->threeTL(menu['陕西省']) -->threeTL(menu['陕西省'])-->threeTL(menu['陕西省']['西安']) -->threeTL(menu['陕西省'])-->threeTL(menu['陕西省']['西安'])-->threeTL(menu['陕西省']['西安']['未央区'])
(2)当用户输入 ' b ':
def threeTL(dic): # dic = dic['陕西省']['西安'] while True: for key in dic: print(key) # 未央莲湖高新 choice = input('>>>').strip() # # 用户输入:b if choice == 'b' or choice == 'q': return choice # 返回'b',程序退出 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) if res == 'q': return 'q' elif not choice or choice not in dic: continue def threeTL(dic): # dic = dic['陕西省'] while True: for key in dic: print(key) # 第二次while循环打印 <西安、咸阳>(第一次while循环是在递归函数深入的时候执行) choice = input('>>>').strip() # 第二次while循环到此,用户输入 b if choice == 'b' or choice == 'q': return choice # 返回'b',程序退出 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) # res = 'b', 本次if循环没有匹配到res退出,进行下一次while循环 if res == 'q': return 'q' elif not choice or choice not in dic: continue def threeTL(dic): while True: for key in dic: print(key) # 第二次while循环打印 <陕西省、四川省>(第一次while循环是在递归函数深入的时候执行) choice = input('>>>').strip() # 第二次while循环到此,用户输入 b if choice == 'b' or choice == 'q': return choice # 返回'b',整个程序退出,递归结束 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) # 递归返回 threeTL(dic['陕西省']) if res == 'q': return 'q' elif not choice or choice not in dic: continue
(3)当用户输入 'q'
def threeTL(dic): # dic = dic['陕西省']['西安'] while True: for key in dic: print(key) # 未央莲湖高新 choice = input('>>>').strip() # # 用户输入:q if choice == 'b' or choice == 'q': return choice # 返回'q',程序退出 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) if res == 'q': return 'q' elif not choice or choice not in dic: continue def threeTL(dic): # dic = dic['陕西省'] while True: for key in dic: print(key) # <西安、咸阳> choice = input('>>>').strip() if choice == 'b' or choice == 'q': return choice elif choice in dic and dic[choice]: res = threeTL(dic[choice]) # res = 'q' if res == 'q': return 'q' # 匹配到 res='q' 返回 'q' 程序退出 elif not choice or choice not in dic: continue def threeTL(dic): # dic = dic['陕西省']['西安'] while True: for key in dic: print(key) # 未央莲湖高新 choice = input('>>>').strip() # # 用户输入:q if choice == 'b' or choice == 'q': return choice # 返回'q',程序退出 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) if res == 'q': return 'q' elif not choice or choice not in dic: continue