作业 3/20

今日作业:
1、函数对象优化多分支if的代码练熟

def top_up():
    print('提现')
def transfer():
    print('转账')
def look_at_it():
    print('查询余额')
dic1 = {
    '0':['退出',exit],
    '1':['提现',top_up],
    '2':['转账',transfer],
    '3':['查询余额',look_at_it]
}
while True:
    for i in range(len(dic1)):
        print('{} {}'.format(i,dic1[str(i)][0]))
    choice = input('请输入编号:').strip()
    if choice.isdigit():
        if dic1.get(choice):
            dic1[choice][1]()
        else:
            print('没有这个命令')
    else:
        print('请输入数字')

2、编写计数器功能,要求调用一次在原有的基础上加一
温馨提示:
I:需要用到的知识点:闭包函数+nonlocal
II:核心功能如下:
def counter():
x+=1
return x

要求最终效果类似
    print(couter()) # 1
    print(couter()) # 2
    print(couter()) # 3
    print(couter()) # 4
    print(couter()) # 5
def couter():
    x = 0
    def counter():
        nonlocal x
        x += 1
        return x
    return counter
couter1 = couter()
print(couter1())
print(couter1())
print(couter1())
print(couter1())
print(couter1())

猜你喜欢

转载自www.cnblogs.com/pythonwl/p/12532601.html