path:可以理解为你选取一个自己需要的网址(通过切片等手段)如:、home/index/xxx/xxxxx/经过切片split,切成 ‘home’,‘index’ ,‘xxx’,‘xxxxx’ 选取你需要的片段
用print是为了方便看结果,实际项目多用return返回值
具体过程看注释
def index(path):
msg='这是{}页面'.format(path)
print(msg)
def home(path):
msg = '这是{}页面'.format(path)
print(msg)
def error(path):
msg = '{}是错误页面!'.format(path)
print(msg)
list=[
('index',index),
('home',home),
]
path=input('请输入切断地址')
func=None#给func赋值为空
for path_list in list:#遍历list列表
if path_list[0]==path:#如果元组的前一个值(下标为0的)等于自己输入的切片地址,则执行下一步
func=path_list[1]#将地址赋值给func
break
if func:
msg=func(path)#作用是调用前面函数值
else:
msg=error(path)