函数返回值做参数,需要注意什么
1 def cond(): 2 "*** YOUR CODE HERE ***" 3 return False 4 5 def true_func(): 6 "*** YOUR CODE HERE ***" 7 print(42) 8 9 10 def false_func(): 11 "*** YOUR CODE HERE ***" 12 print(47) 13 14 15 def if_function(condition, true_result, false_result): 16 """Return true_result if condition is a true value, and 17 false_result otherwise. 18 19 >>> if_function(True, 2, 3) 20 2 21 >>> if_function(False, 2, 3) 22 3 23 >>> if_function(3==2, 3+2, 3-2) 24 1 25 >>> if_function(3>2, 3+2, 3-2) 26 5 27 """ 28 if condition: 29 return true_result 30 else: 31 return false_result 32 33 def with_if_function(): 34 """ 35 >>> result = with_if_function() 36 42 37 47 38 >>> print(result) 39 None 40 """ 41 return if_function(cond(), true_func(), false_func()) 42 43 44 result=with_if_function() 45 #竟然是代入函数时就他妈的打印出函数了, 46 #变量(参数)代入是函数的返回值,所以首先要运行函数(true_func,false_func),于是就打印了值,但是if_function的参数是值 47 #,可是函数返回值不存在。于是retrun 不存在,也就是什么都没有打印 48 49 #if_statement 没有任何变量(也就是参数),所有没有运行函数的过程 50 #但是return 的时候,有一个函数的调用,于是打印了一次 51 print("Not there is new edition.") 52 def with_if_statement(): 53 """ 54 >>> result = with_if_statement() 55 47 56 >>> print(result) 57 None 58 """ 59 if cond(): 60 return true_func() 61 else: 62 return false_func() 63 64 newresult=with_if_statement()
编译结果:
42
47
Not there is new edition.
47
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)