I built the python code that can find the sentence that is in another sentence as below, but it didn't work well.
sentence = "While speaking to Ross, Rachel comes to terms with something that was bothering her."
if "Rachel has made coffee to Joey and Chandler for the first time of her entire life." or "Monica can't stop smiling while having a conversation with Rachel." in sentence:
print("YES")
else
print("NO!")
It should be printed "NO!"
because it has totally different sentence. However, it prints "YES"
..
Is this because of string?
Is there anything I did wrong in my code or
Do I misunderstand something?
You are not using the or
correctly you should -
if "Rachel has made coffee to Joey and Chandler for the first time of her entire life." in sentence or "Monica can't stop smiling while having a conversation with Rachel." in sentence:
The if condition returns False
if the variable is None or empty list or empty string or empty set or empty dictionary (...) and True
otherwise.
Your problem is, that or
is a boolean operator. It does not operate on Strings, but on expressions like string in string
. Try something like this:
if ("Rachel has made coffee to Joey and Chandler for the first time of her entire life." in sentence)or ("Monica can't stop smiling while having a conversation with Rachel." in sentence):
Take a look at this example:
if "some string":
print("YES")
else:
print("NO")
if you run this in your environment the if clause will always evaluate to True
and an output of "YES" will be shown.
Why? Because the string is not being compared to anything and therefore can never be evaluated as a False
statement
Now let's take a look at the if clause in your code(With minor formatting changes):
sentence = "While speaking to Ross, Rachel comes to terms with something that was
bothering her."
text1 = "Rachel has made coffee to Joey and Chandler for the first time of her entire life."
text2 = "Monica can't stop smiling while having a conversation with Rachel."
if (text1) or (text2 in sentence):
print("YES")
else:
print("NO")
When using the logical or
operator the if clause evaluates as True
if either or both of the conditions are met.
text1 is not compared to anything and returns True
automatically, the program enters the if clause and executes your print statement
Instead we could re-write the code as follows:
if (text1 in sentence) or (text2 in sentence):
We evaluate if text1 or text2 are substrings of sentence.
Your comparison statement is a bit faulty. You are basically saying this:
if string or other_string in comp_string:
The first part of the conditional 'if string' always evaluates to true. You are not checking whether that string exists in the string you want to compare to, which is why you are always printing 'YES'.
You need to be more explicit. What you want to do is this:
if string in comp_string or other_string in comp_string:
This should evaluate correctly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.