在bothon中将布尔函数传递给if-condition

时间:2014-05-11 02:54:44

标签: python

我正在学习python,我试图这样做,我认为这应该是微不足道的,但显然,它并非如此。

$python
>>> def isTrue(data):
...     "ping" in data
... 
>>> if isTrue("ping"):
...     print("pong")       >>>>>>>>>>>>>> shouldn't this print "pong"???
... 
>>>

1 个答案:

答案 0 :(得分:1)

您实际上需要返回该值。 然后,它会起作用。

>>> def isTrue(data):
...     return "ping" in data
... 
>>> if isTrue("ping"):
...     print("pong")
...
pong
>>>