Python 3.x 显示IF语句的函数与IF语句的工作方式不同
这是一个我无法理解的家庭作业问题: 问题开始 第三季度。让我们尝试编写一个函数,该函数与if语句的功能相同:Python 3.x 显示IF语句的函数与IF语句的工作方式不同,python-3.x,Python 3.x,这是一个我无法理解的家庭作业问题: 问题开始 第三季度。让我们尝试编写一个函数,该函数与if语句的功能相同: 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 el
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
实际上,在所有情况下,该函数与if语句的作用并不相同。为了证明这一事实,编写函数c、t和f,使其中一个函数返回数字1,而另一个函数不返回:
def with_if_statement():
if c():
return t()
else:
return f()
def with_if_function():
return if_function(c(), t(), f())
问题结束
以下是我得出的结论:
如果c()为真,则with if_语句()不计算f(),但如果with if_函数()在检查c()是否为真之前计算所有3个值
因此,我考虑在c()中指定一个全局变量,并在f()中更改其值
这是我的代码(不起作用):
有人能帮我找出答案吗?谢谢 全局
语句不应抛出名称错误(因此您不会在c()中运行x=1
)。我会尝试在不使用异常的情况下重写您的代码,它们将不必解决这个问题,并且使它变得比需要的更复杂。使用全局变量并在函数中产生副作用肯定是正确的
def if_function(condition, true_result, false_result):
"""Return true_result if condition is a true value, and
false_result otherwise.
>>> if_function(True, 2, 3)
2
>>> if_function(False, 2, 3)
3
>>> if_function(3==2, 3+2, 3-2)
1
>>> if_function(3>2, 3+2, 3-2)
5
"""
if condition:
return true_result
else:
return false_result
def with_if_statement():
"""
>>> with_if_statement()
1
"""
if c():
return t()
else:
return f()
def with_if_function():
return if_function(c(), t(), f())
问题要求编写3个函数:c
、t
和f
,这样with_if_语句
返回1
,而with_if_函数
不返回1(它可以做其他任何事情)
一开始,这个问题似乎很可笑,因为从逻辑上讲,with_if_语句
和with_if_函数
是相同的。然而,如果我们从解释器的角度来看这两个函数,它们是不同的
如果函数
应用于结果参数,则带函数
的函数
使用调用表达式,这保证在将函数
应用于结果参数之前对其所有操作数子表达式求值。因此,即使c
返回False
,也将调用函数t
。相比之下,with_if_语句
永远不会调用t
ifc
返回False(本段来自UCB网站)
我已经修正了你的格式。将代码缩进四个空格(Ctrl+K或相应的工具栏按钮)。根据HTML,
标记是一个内联标记,您不需要在这里使用它。在这两种情况下,如果首先调用了_if_statement()和_if_function()c(),则都可以使用它。因此,c()必须给x赋值。然后,我想更改f()中x的值,并且在调用c()时不要再更改它。我该怎么做呢?在c()
中,您可以将x
的值与2(在f()
中指定的值)进行比较。但是我得到了错误:全局变量x没有为thsi代码定义:def c():全局x如果x=2:x=1如果x==1:return True else:return False同样,我仍然不知道代码格式如何处理堆栈溢出…使用global x
语句不会创建全局变量。它只是提供对函数中全局变量的访问,因此当您为其赋值时,赋值将转到全局变量,而不是新的局部变量。上一条注释中的代码的问题是,如果您没有分配x=1
,那么当您将x
与x==1
进行比较时,x
仍然不存在。解决这个问题的一个简单方法是将x=0
粘贴在脚本的顶部,这样它就会一直存在;没有别的了,那么如何在开始时指定x=0呢?我想我要找的是一个“isset()”类型的函数(如php中的函数)。通过谷歌搜索,我发现try也有同样的功能。但这不起作用,因此我不知道该怎么办。我确信我只是把事情复杂化了。我只需要用同样简单的方法在c()中生成一个全局变量x,仅当它没有被赋值时才给它赋值(在c()中),如果x=1,则使c()返回true,否则返回false,然后在f()中更改它的值,使c()返回false。但是如何?
def if_function(condition, true_result, false_result):
"""Return true_result if condition is a true value, and
false_result otherwise.
>>> if_function(True, 2, 3)
2
>>> if_function(False, 2, 3)
3
>>> if_function(3==2, 3+2, 3-2)
1
>>> if_function(3>2, 3+2, 3-2)
5
"""
if condition:
return true_result
else:
return false_result
def with_if_statement():
"""
>>> with_if_statement()
1
"""
if c():
return t()
else:
return f()
def with_if_function():
return if_function(c(), t(), f())
def c():
return True
def t():
return 1
def f():
'1'.sort() # anything breaks the program is OK