简体   繁体   English

TypeError: 'str' 对象在使用列表时不可调用

[英]TypeError: 'str' object is not callable when using lists

I'm trying to get it so that if the user enters something from the list, it will print the message.我正在尝试获取它,以便如果用户从列表中输入某些内容,它将打印消息。 However, I'm met with a然而,我遇到了一个

"TypeError: 'str' object is not callable". “类型错误:‘str’对象不可调用”。

li = "One,Two,Three,Four"
a,b,c,d = li.split(",")
number = input("Enter Number")

if input == li(a,b,c,d):
    print("Message")

If you want to check if an object is one of the elements of a list, you need to use the in operator, not the == operator:如果要检查对象是否是列表的元素之一,则需要使用in运算符,而不是==运算符:

if number in (a,b,c,d):
    # Here^
    print("Message")

There's no need to define the entries together just to separate them later;没有必要将条目定义在一起只是为了以后将它们分开; also you can directly check if an entry is in your list:您也可以直接检查条目是否在您的列表中:

entries = ["One", "Two", "Three", "Four"]
number = input("Enter Number: ")
if number in entries:
    print("Message")

For large datasets consider using a set instead of a list for entries.对于大型数据集,请考虑使用set而不是条目列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM