@2020.3.20
1
def foo():print('foo')def bar():print('bar')
dic={'foo':foo,'bar':bar,
}while True:
choice=input('>>: ').strip()if choice in dic:
dic[choice]()
2、编写计数器功能,要求调用一次在原有的基础上加一 温馨提示: I:需要用到的知识点:闭包函数+nonlocal II:核心功能如下:def counter(): x+=1return x 要求最终效果类似print(couter()) # 1print(couter()) # 2print(couter()) # 3print(couter()) # 4print(couter()) # 5
def f1(): x=0def counter(): nonlocal x x+=1return xreturn counter counter=f1()print(counter())print(counter())print(counter())print(counter())print(counter())
Copyright © 2004-2024 Ynicp.com 版权所有 法律顾问:建纬(昆明)律师事务所 昆明市网翼通科技有限公司 滇ICP备08002592号-4