The issue you're running into has nothing to do with the bot, it's how Python's boolean evaluation works.
(您遇到的问题与该机器人无关,这是Python的布尔值评估的工作方式。)
If you run a binary operator like or
or and
, then Python will return the first thing that determines its truth value. (如果运行像or
或and
这样的二进制运算符,则Python将返回确定其真值的第一件事。)
For x and y
, Python will return False
if x
evaluates to false and if x
evaluates to True
, Python will return the value of y
, as this determines the truth value of the and
statement.
(对于x and y
,如果x
计算结果为false,则Python将返回False
;如果x
计算结果为True
,则Python将返回y
的值,因为这将确定and
语句的真值。)
Given that a non-empty string evaluates to True
, the output in your example will always be determined by the second string.
(给定一个非空字符串的值为True
,示例中的输出将始终由第二个字符串确定。)
In [1]: True and 'bot'
Out[1]: 'bot'
In [2]: 'bot' and True
Out[2]: True
In [3]: 'bot' and 'not'
Out[3]: 'not'
In [4]: 'not' and 'bot'
Out[4]: 'bot'
Therefore, your "bot" and "not"
evaluation gives you "not"
and the bot will react to any message that has the word "not" in it.
(因此,您的"bot" and "not"
评估结果为"not"
并且该bot将对其中带有单词“ not”的任何消息作出反应。)
You need to change your condition to check for both words individually and react only if both checks pass
(您需要更改条件以单独检查两个单词,并且仅在两个检查均通过时才做出反应)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…