13. recursive function

  1. Recursive function: function or other code can be resolved

Official website states: The default maximum depth of recursion 1000. If you have not more than 100 times the recursive solution to this problem, then insisted on the use of recursion is very low efficiency.

import sys
print(sys.setrecursionlimit(1000000))

def func(n):
    print(n)
    n += 1
    func(n)
func(1)

Computing Age:

def age(n):
    if n == 1:
        return 18
    else:
        return age(n-1) + 2

print(age(4))
'''
def age(4):
    if n == 1:
        return 18
    else:
        return age(3) + 2

age(4) = age(3)  + 2

def age(3):
    if n == 1:
        return 18
    else:
        return age(2) + 2
age(4) = age(2) + 2  + 2


def age(2):
    if n == 1:
        return 18
    else:
        return age(1) + 2
age(4) = age(1) + 2 + 2  + 2    

def age(1):
    if n == 1:
        return 18
    else:
        return age(1) + 2

age(4) = 18 + 2 + 2  + 2    
'''

Print a list of all the elements:

 l1 = [1, 3, 5, ['小王','小红', 34, [33, 55, [11,33]]], [77, 88],66]
 def func(alist):
    for i in alist:
        if type(i) == list:
            func(i)
        else:
            print(i)
func(l1)

.

Guess you like

Origin www.cnblogs.com/pythonblogs/p/11135199.html