if
ステートメントを実行しようとしていますが、何らかの理由で動作しません。それは非常に簡単なはずです。用(文字列または文字列または文字列)が変数にない場合
if "Today" in title or "sea" in title:
print 1
(似た:
if "Today" or "sea" in title:
print 1
if "Today" or "sea" not in title:
print 1
どちらの仕様は1
if
ステートメントを実行しようとしていますが、何らかの理由で動作しません。それは非常に簡単なはずです。用(文字列または文字列または文字列)が変数にない場合
if "Today" in title or "sea" in title:
print 1
(似た:
if "Today" or "sea" in title:
print 1
if "Today" or "sea" not in title:
print 1
どちらの仕様は1
変更し、これにあなたのコードにつながる:
は、文字列がtitle = "Today I went to the sea"
私のコードがあると仮定しますコードの2番目の部分)。
どのようにif
文が動作するかは、or
またはand
のような単語で結合された式を評価することです。だからあなたの文は次のように読んでいた:truthyある
if ("Today") or ("sea" in title):
print 1
"Today"
ので、それは常に私は、これはSOにどこかに回答されていると、あなたが最初に新しい質問をする前に、そこにチェックする必要があります保証true
に評価しました。 。
あなたのコードの問題:
if 'Today' or 'sea' in title:
このチェック「今日は」Trueの場合は「海」はタイトルにある場合は、「今日==タイプ(文字列)ので、それは真/存在は、 " sea 'title == Trueと評価された場合、if 'today' or 'sea' not in title
' today 'は再びタイプ(文字列)なので、/ Trueと' sea 'はtitle = Falseではなく、Trueと評価されます。これを修正する方法! if 'Today' in title or 'Sea' in title:
またはそれ以下を簡単に変更可能にする!がんばろう!
strings_to_compare = ['Today', 'sea', 'etc...']
for i in strings_to_compare:
if i in title:
print i
なぜダウン票ですか? – winhowes