I have written a solution for the below question.
Q) 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 does not 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 the 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():
"*** YOUR CODE HERE ***"
def t():
"*** YOUR CODE HERE ***"
def f():
"*** YOUR CODE HERE ***"
Solution:
>>> def c():
return 2 < 3
>>> def t():
return 2 < 3
>>> def f():
return not 2 < 3
>>> print(with_if_function())
True
>>>
My question:
Can you please confirm, if my solution is correct?
or
Do you think am yet to understand this question correctly?
ifin it?ifis a statement andif_functionis an expression. The latter can be used in places the former cannot.