day16

1、函数对象优化多分支if的代码练熟

def login():
    pass

def check_balance():
    pass

def withdraw():
    pass

def transfer():
    pass

def check_flow():
    pass

def logout():
    pass

def register():
    pass

func_dic = {
    '0':('退出', logout),
    '1':('登录',login),
    '2':('查看余额',check_balance),
    '3':('提现', withdraw),
    '4':('转账', transfer),
    '5':('查看流水', check_flow),
    '6':('注册', register)
}

def run():
    while True:
        for i in func_dic:
            print(i, func_dic[i][0])
        choice = input('请输入功能编号:').strip()
        if choice == '0':
            break
        elif not choice.isdigit():
            print('请输入数字!')
            continue
        elif choice not in func_dic:
            print('请输入正确的功能编号')
            continue
        else:
            func_dic.get(choice)[1]()

if __name__ == '__main__':
    run()

​ 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 func(x):
    def counter():
        nonlocal x
        x += 1
        return x
    return counter

counter = func(0)
print(counter()) #1
print(counter()) #2
print(counter()) #3
print(counter()) #4
print(counter()) #5

posted @ 2020-03-20 18:18  蛋蛋的丶夜  阅读(123)  评论(0)    收藏  举报