python怎么返回true_python没有返回true

博客讨论了一个Python函数的逻辑结构,该函数包含嵌套条件和递归调用。当condition3为真时,函数会打印"text",但不会触发外部if语句的"ok"打印。作者寻求帮助理解预期行为与实际行为之间的差异,并提供了可能的解决方案,如插入打印语句或使用调试器来定位问题。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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"

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

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

On Feb 14, 4:15 pm, "agent-s"

I have a function, generally described as so: def function(args):

if condition:

if condition2:

function(args+1)

return None

elif condition3:

print "text"

return True

else:

return False

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.

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"

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

On Tue, 13 Feb 2007 21:15:19 -0800, agent-s wrote:

I have a function, generally described as so:

[snip function]

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"

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

--

Steven D''Aprano

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值