python没有返回true [英] python not returning true

查看:283
本文介绍了python没有返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,一般描述如下:


def功能(args):

如果条件:

如果条件2:

函数(args + 1)

elif condition3:

打印" text"

return真正的

否则:

返回False


用于:


if function(args):

print" ok"

所以这里基本上是text当条件3为真时会打印出来但是它不会打印出来确定当condition3为true时。如果它是真的那么

应打印出来的文字text和ok

I have a function, generally described as so:

def function(args):
if condition:
if condition2:
function(args+1)
elif condition3:
print "text"
return True
else:
return False

which is used in:

if function(args):
print "ok"
so here basically "text" will print out when condition3 is true but it
will not print out "ok" when condition3 is true. When it''s true it
should print out borth "text" and "ok"

推荐答案

" agent-s" < sh ******* @ gmail.comwrites:
"agent-s" <sh*******@gmail.comwrites:

我有一个功能,一般描述如下:


def函数(args):

如果条件:

如果条件2:

函数(args + 1)

elif condition3:

打印" text"

返回True

else:

返回False
I have a function, generally described as so:

def function(args):
if condition:
if condition2:
function(args+1)
elif condition3:
print "text"
return True
else:
return False



你已经简化了这个,大概是为了让代码更加清晰。不幸的是,剩下的是不可执行的,所以我们看不到让你困惑的行为。


请写一个最小的例子来证明行为你

想要解释。


-

\我怀疑,因此我可能会。 - 匿名|

` \ |

_o__)|

Ben Finney

You''ve simplified this, presumably to make the code more
clear. Unfortunately what remains isn''t executable, so we can''t see
the behaviour that confuses you.

Please write a minimal example that demonstrates the behaviour you
want explained.

--
\ "I doubt, therefore I might be." -- Anonymous |
`\ |
_o__) |
Ben Finney


2月14日下午4:15,agent-s < shanek ... @ gmail.comwrote:
On Feb 14, 4:15 pm, "agent-s" <shanek...@gmail.comwrote:

我有一个函数,一般描述如下:
I have a function, generally described as so:


def函数(args):

如果条件:

如果条件2:

函数(args + 1)
def function(args):
if condition:
if condition2:
function(args+1)



返回无

return None


elif condition3:

print" text"

返回True

否则:

返回False
elif condition3:
print "text"
return True
else:
return False



else:

返回无


有两种情况,如上所示,你没有明确地做一个

返回,所以你失去了函数的结尾,Python返回

无。


然后当函数的调用者测试返回的值时,None是

视为逻辑错误。

else:
return None

There are two cases, indicated above, where you don''t explicitly do a
"return", so you fall off the end of the function, and Python returns
None.

Then when the function''s caller tests the returned value, None is
treated as logically false.


用于:


如果函数(args):

打印ok


so这里基本上是文本当条件3为真时会打印出来但是它不会打印出来确定当condition3为true时。如果它是真的那么

应打印出来的文字text和确定
which is used in:

if function(args):
print "ok"

so here basically "text" will print out when condition3 is true but it
will not print out "ok" when condition3 is true. When it''s true it
should print out borth "text" and "ok"



在倒数第二句中,很难确定你认为预期行为是什么,以及你说的实际行为是什么。

在最后一句中,第一个它是什么?请参阅?


如果关于返回None的知识对你没有帮助,可以尝试一些

标准(??)技术,比如插入print语句或调试器

断点。


HTH,

John

In the second last sentence, it is difficult to determine what you
think is expected behaviour and what you say is the actual behaviour.
In the last sentence, what does the first "it" refer to?

If the knowledge about returning None doesn''t help you, try some
standard(??) techniques like inserting print statements or debugger
break-points.

HTH,
John

2007年2月13日星期二21:15:19 -0800,agent-s写道:
On Tue, 13 Feb 2007 21:15:19 -0800, agent-s wrote:

我有一个函数,一般描述如下:
I have a function, generally described as so:



[snip function]

[snip function]


用于:


if function(args):

print" ok"


所以这里基本上是text当条件3为真时会打印出来但是它不会打印出来确定当condition3为true时。如果它是真的那么

应打印出来的文字text和确定
which is used in:

if function(args):
print "ok"

so here basically "text" will print out when condition3 is true but it
will not print out "ok" when condition3 is true. When it''s true it
should print out borth "text" and "ok"



感谢您的分享。你有实际问题吗?


-

Steven D''Aprano

Thank you for sharing. Do you have an actual question?


--
Steven D''Aprano


这篇关于python没有返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆