检查Python中的成员资格
在Python3中,它正在工作检查Python中的成员资格,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,在Python3中,它正在工作 a=["Abhi", "Gupta"] eval(input("Please enter a string: ")) in a Please enter a string: "Abhi" => True 但在Python2中,我得到了以下错误 a=["Abhi", "Gupta"] raw_input("Please enter a string: ") in a Traceback (most recent call last): File "&l
a=["Abhi", "Gupta"]
eval(input("Please enter a string: ")) in a
Please enter a string: "Abhi"
=> True
但在Python2中,我得到了以下错误
a=["Abhi", "Gupta"]
raw_input("Please enter a string: ") in a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not callable
有人能解释一下为什么它在Python 2中不起作用,以及这个“bool”对象是什么吗?对于Python 3示例,eval是不必要的,因为输入返回str
至于Python2中的TypeError,您必须在该行之前将raw_输入重写为bool类型。实际上,评估是必要的,或者类似但更安全的东西,比如ast.literal,因为提供的输入包括引号;如果没有eval,它会在['Abhi',…]中检查'Abhi',引号会阻止它找到匹配项。如果输入没有引号,那么是的,输入单独起作用。@ShadowRanger哦,好的,抓住了!我想我应该对OP说:在输入stringsIt时不要加引号。对于我来说,Python2.7.13似乎工作得很好。