zoukankan      html  css  js  c++  java
  • 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
    

  • 相关阅读:
    SCRUM第一天
    第八周总结
    第7周总结
    团队项目nabcd
    人月神话1
    课堂练习之四则运算
    第六周总结
    Storm Grouping —— 流分组策略
    抓取网页内容生成Kindle电子书(转)
    浅析PageRank算法(转)
  • 原文地址:https://www.cnblogs.com/xy-han/p/12533488.html
Copyright © 2011-2022 走看看