如何使用 if 语句来测试字符串的成员资格

how to use if statement to test membership of a string

我无法理解简单的 Python 语句中的逻辑。我正在尝试使用 'not in' 来测试 's' 或 '5' 是否是输入的一部分,但是当我使用它们中的任何一个时,都会执行相同的打印语句,即 's or 5 is not included'。这是我的代码:

myinput = input('Enter input here')
if 's' or '5' not in myinput:
    print('s or 5 is not included')
else:
    print('s or 5 is included')

有人能帮帮我吗?谢谢

(确定这是重复的,但找不到。)

"Neither 's' nor '5' are in myinput" 在逻辑上等同于“('s' 不在我的输入中)和('5' 不在我的输入中)”。 (参见德摩根定律。)

正如斯科特·亨特所说

if ('s' not in myinput) and ('5' not in myinput):
    print('s or 5 is not included')
else:
    print('s or 5 is included')

De Morgan 定律指出 not (A or B) = 不是 A 也不是 B

在您的示例中,A = 's' 在 myinput 中,B = '5' 在 myinput