python新手入门---函数

    xiaoxiao2023-11-20  160

    2019/5/25 鱼C教程19、20课 全局变量与局部变量

    def discounts(price,rate): final_price = price * rate return final_price old_price = float(input('请输入原价:')) rate = float(input('请输入折扣率:')) new_price = discounts(old_price,rate) print('打折后价格是:',new_price)

    =================================

    count = 5 #全局 def Myfun(): count = 10 #局部 print(10) Myfun(count)

    5 #优先全局

    ====================================

    count = 5 def Myfun(): global count = 10 #global强制全局 print(10) Myfun(count)

    10

    函数嵌套 闭包

    def funx(x): def funy(y): return x*y return funy

    funx(8)(5) 40 i = fun(8) i= <function funx..funy at >

    关键字: nonlocal->强制转换为非局部变量

    最新回复(0)