Python IF True/False



当"Python"如果在文本中检测到(或未检测到),则输出True或False。现在我想用它们来打印不同的语句,比如&;is there&;或者"不是"但它不工作。

Text = "Python for beginners"
print("Python" in Text)
if Text == True:
print("Its there!")
else:
print("its not there")

问题可能是与if Text == True:声明,但我只是不能得到它的权利。我已经尝试过if Text is True:if Text == "True":,但是没有任何效果。所以如果这里有人能帮我,我会很高兴的。

您在打印语句"Python" in text中有正确的检查,您只是没有以正确的方式使用它。您可以将值存储在一个变量中:

is_found = "Python" in text
if is_found:
print("Its there!")
else:
print("its not there")

或者直接在条件句中使用:

if "Python" in text:
print("Its there!")
else:
print("its not there")

,但是如果只打印该值,如果不重复相同的检查,则无法在稍后的条件中访问该值。

您只打印您之前做的支票,您可能需要这样的内容:

Text = "Python for beginners"
if "Python" in Text:
print("Its there!")
else:
print("its not there")

python会运行

"Python" in Text

作为将返回True的表达式,因此它将进入if语句,并打印Its there!

Text = "Python for beginners"
if "Python" in Text:
print("Its there!")
else:
print("Its not there")

现在它将打印。

最新更新