随笔- 154  文章- 0  评论- 5  阅读- 42310 

作业12

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

def login():
    print("登录")


def logon():
    print("注册")


def with_draw():
    print("提现")


def check_balance():
    print("查询余额")


def transfer():
    print('转账')


func_dic = {
    "0": ["退出", exit],
    "1": ["登录", login],
    "2": ["注册", logon],
    "3": ["提现", with_draw],
    "4": ["查询余额", check_balance],
    "5": ["转账", transfer]
}

while 1:
    for n,f in func_dic.items():
        print(n,f[0])
    command = input("请输入要选择的功能:")
    if not command.isdigit():
        print("必须输入数字")
        continue
    if command in func_dic:
        func_dic[command][1]()
    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 wrapper(x):
    def counter():
        nonlocal x
        x += 1
        return x
    return counter

counter = wrapper(0)
print(counter())
print(counter())
print(counter())
print(counter())
print(counter())

1

 posted on   wwwpy  阅读(137)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示