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

问题描述 投票:0回答:2

我在用简单的Python语句理解逻辑时遇到麻烦。我试图使用“ not in”来测试“ s”或“ 5”是否是输入的一部分,但是当我使用它们中的任何一个时,都会执行相同的打印语句,其中显示“ s或5不包括在内”。这是我的代码:

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')

有人可以帮我吗?谢谢

string if-statement membership
2个回答
0
投票

如斯科特·亨特所说

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

德摩根定律指出不是(A或B)=不是A而不是B

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


0
投票

(肯定是重复的,但找不到它。)

“ myinput中既不是's'也不是'5'在逻辑上等效于”(s's不在myinput中)和('5'不在myinput中)“。 (请参阅德摩根定律。)

© www.soinside.com 2019 - 2024. All rights reserved.