早教吧 育儿知识 作业答案 考试题库 百科 知识分享

Python,Q3.Let'strytowriteafunctionthatdoesthesamethingasanifstatement:defiffunction(condition,trueresult,falseresult):"""Returntrueresultifconditionisatruevalue,andfalseresultotherwise."""ifcondition:returntrueresultel

题目详情
Python,
Q3.Let's try to write a function that does the same thing as an if statement:
def if_function(condition,true_result,false_result):
"""Return true_result if condition is a true value,and false_result otherwise."""
if condition:
return true_result
else:
return false_result
This function actually doesn't do the same thing as an if statement in all cases.To prove this fact,write functions c,t,and f such that one of these functions returns the number 1,but the other does not:
def with_if_statement():
if c():
return t()
else:
return f()
def with_if_function():
return if_function(c(),t(),f())
▼优质解答
答案和解析
def c():return 0
def t():return 0
def f():return 1
英文翻译起来我倒是没什么难度,但是这道题的意思我却没看太懂.上边三行是我按最可能的意思写出来的,其实,这道教材里的思考题,我觉得,应该是面向零基础编程者的一个小篇章,意思让大家知道,通过if语句,一个函数可以根据条件的不同而输出不同的值.这个意思领会了就好,其实很简单.
我Q 103349907,突然发觉,很久没玩Python了.