Python基礎之函式的巢狀

芒果芋猿發表於2020-11-30

一、函式巢狀的定義

# 函式的巢狀
def f1():
    print('from f1')
    def f2():
        print('from f2')
    # print(f2)
    f2()
    x=11111
f1()

# 計算圓的周長面積
from math import pi

def circle(radius,mode=0):
    def perimiter(radius):
        return 2 * pi * radius

    def area(radius):
        return pi * (radius ** 2)

    if mode == 0:
        return perimiter(radius)
    elif mode == 1:
        return area(radius)

res=circle(10,mode=1)
print(res)

二、函式巢狀的呼叫

def max2(x,y):
    if x > y:
        return x
    else:
        return y


def max4(a,b,c,d):
    res1=max2(a,b)
    res2=max2(res1,c)
    res3=max2(res2,d)
    return res3

res=max4(1,2,3,4)
print(res)

相關文章