简体   繁体   中英

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".

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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