Python根据布尔变量调用其他函数时外层函数无返回值如何解决?
问题修复方案
核心问题说明
代码存在两个可直接修复的问题:
function3没有显式返回调用function1/function2得到的结果,Python函数无return语句时默认返回None,这是拿不到返回值的核心原因- 所有函数定义行末尾缺少Python语法要求的冒号
:,实际运行会直接触发语法错误
修正后代码
def function1(n): result1 = do things to (n) return result1 def function2(n): result2 = do other things to (n) return result2 def function3(n, aboolvariable=True): if aboolvariable: return function1(n) else: return function2(n)
修改后调用function3即可正常拿到对应子函数的返回结果。
内容的提问来源于stack exchange,提问作者newbieee
相关产品推荐
相关产品推荐

