3.20作业

# 1、函数对象优化多分支if的代码练熟
# def login():
# print('登录功能')
#
# def transfer():
# print('转账功能')
#
# def check_balance():
# print('查询余额')
# def withdraw():
# print('提现')
# def register():
# print('注册')
#
# func_dic={ '0':['退出',None],
# '1':['登录',login],
# '2':['转账',transfer],
# '3':['查询',check_balance],
# '4':['提现',withdraw],
# '5':['注册',register]
# }
# while True:
# for i in func_dic:
# print(i,func_dic[i][0])
# choice=input('请输入命令:')
# if not choice.isdigit():
# print('必须输入数字')
# continue
# if choice==0:
# break
# if choice in func_dic:
# func_dic[choice][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 func():
x=0
def counter():
nonlocal x
x+=1
return x
return counter

counter=func()
print(counter())
print(counter())
print(counter())
print(counter())
print(counter())

猜你喜欢

转载自www.cnblogs.com/chenyoupan/p/12534033.html