zoukankan      html  css  js  c++  java
  • 作业 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())
    
  • 相关阅读:
    The library 'hostpolicy.dll' required to execute the application was not found in
    矩阵乘法
    2019-11-1
    四边形不等式的应用
    2019-10-30
    2019-10-29
    差分与前缀和
    平衡树SPLAY
    可爱的树链剖分(染色)
    cable tv network
  • 原文地址:https://www.cnblogs.com/pythonwl/p/12532601.html
Copyright © 2011-2022 走看看