當在文本中檢測到 "Python "時(或不檢測),它輸出True或False。 現在我想用這些來列印不同的陳述句,如 "是否存在 "或 "是否存在",但是 但這并不奏效。
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":但都沒有成功。所以如果有人能幫助我,我真的很高興
uj5u.com熱心網友回復:
你在列印陳述句中有正確的檢查"Python "在文本中,你只是沒有以正確的方式使用它。你可以把值存盤在一個變數中:
is_found = "Python" in text
if is_found:
print("its there!")
else:
print("its not there")
或者直接在條件中使用它:
if "Python"/span> in text:
print("its there!")
else:
print("its not there")
但是如果只是列印這個值,你就無法在以后的條件中訪問這個值而不重復同樣的檢查。
uj5u.com熱心網友回復:
你只是在列印你之前做的檢查,你會想要這樣的東西:
你只是在列印你之前做的檢查。
Text = "Python for beginners"
if "Python" in Text:
print("its there!")
else:
print("its not there")
這里發生的事情是Python將運行
"Python"/span> in Text
作為一個運算式,它將回傳True,因此它將進入if陳述句,并列印Its there!.
uj5u.com熱心網友回復:
Text = "Python for beginners"
if "Python" in Text:
print("its there!")
else:
print("its not there")
現在它將列印。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/314524.html
標籤:
