早教吧作业答案频道 -->其他-->
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())
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了.
def t():return 0
def f():return 1
英文翻译起来我倒是没什么难度,但是这道题的意思我却没看太懂.上边三行是我按最可能的意思写出来的,其实,这道教材里的思考题,我觉得,应该是面向零基础编程者的一个小篇章,意思让大家知道,通过if语句,一个函数可以根据条件的不同而输出不同的值.这个意思领会了就好,其实很简单.
我Q 103349907,突然发觉,很久没玩Python了.
看了Python,Q3.Let's...的网友还看了以下:
in good condition 和 in condition 有区别吗 2020-05-16 …
in a condition和 on a condition的区别 2020-05-16 …
用python做一个判断多个数字是否为偶数或奇数的程序用python做一个判断4个数字是否为偶数或 2020-05-22 …
condition可数还是不可数condition的意思是条件,状况,环境,做条件解时到底是可数还 2020-06-11 …
condition是可数名词吗?Shehelpedimproveprisonconditionsa 2020-06-11 …
Python中的%是求余还是求模,在Python中计算-7%4=1;这明明是求模,但是书(针对Py 2020-07-07 …
请教各位朋友,下文中的CHARGEBALBASIS和WARRENTEETERMS&CONDITIO 2020-07-09 …
Python,Q3.Let'strytowriteafunctionthatdoesthesamet 2020-10-30 …
python问题有一个函数Mylog(x,b),假如x是16,b是2,该函数要返还的值就是4,因为2 2020-12-26 …
condition,situation,circumstances的区别特别是都当情况讲时单选题:- 2021-01-01 …