调用不同函数
实现方式一
功能: 根据输入条件的不同选择执行不同的函数,函数的输入参数一致
解决方式:
python通过字典选择调用的函数-定义一个字典,根据字典的值来进行执行函数
函数是一个中要的对象,然后
字典可以使用get方法来进行值的选取,或者使用[]
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# ---------------------------
# CreateTime: 2021/6/11 11:27
# FileName: main_eg
# Author:
def get_upper(input_str):
return input_str.upper()
def get_lower(input_str):
return input_str.lower()
functions_dict = {
"lower": get_lower,
"upper": get_upper,
}
def get_result(choice, input_str):
name_func = functions_dict[choice]
out_value = str(name_func(input_str))
print(out_value)
return out_value
if __name__ == "__main__":
input_str_a = "Study"
get_result("upper", input_str_a)