简体   繁体   中英

How do I find a keyword from user's input in python?

key_words = ("screen", "power", "wifi")

user_input = input("Type: ")

if user_input in key_words:
    print ("you should do this...")

When the user types in anything in key_words it will work, but if the user enters it in the sentence its works like this:

Type: screen is not working
>>> 

It's supposed to find the keyword "screen" and enter yes but it just goes blank. I know I have to split the user's response but how would I do this for the recent python?

This looks like a good job for any . You want to iterate over your sentence and check to see if there exists a word in that list. If there is "ANY" match, return true:

key_words = ("screen", "power", "wifi")

user_input = input("Type: ")

if any(i in key_words for i in user_input.split()):
    print("you should do this...")

You also do not need to case to str as it will already give you a string. So I removed that, it is unnecessary.

As mentioned in the comment, you do in fact have a syntax problem at the end of your conditional statement.

Since split() returns a list and not a single value, you must test each of its elements individually (in a loop).

key_words = ("screen", "power", "wifi")
user_input = input("Type: ")

for word in user_input.split():
  if word in key_words:
    print ("you should do this...")

If the user enters more than one of these keywords, multiple messages will be printed.

Nb this is for python3. For python2, use raw_input instead. I also removed the str() in the input() function.

The solution can be achieved by converting both the key_words and user_input sentence to a set and finding intersection between the 2 sets

key_words = {"screen", "power", "wifi"}

user_input = raw_input("Type: ")

choice = key_words.intersection(user_input.split())
if choice is not None:
    print("option selected: {0}".format(list(choice)[0]))

Output:

Type: screen is not working
option selected: screen

Type: power
option selected: power

Here's what I used:

key_words = ("screen", "power", "wifi")
user_input = input("Type: ")

for word in user_input.split():
  if word in key_words:
    print ("you should do this...")
key_words = ("screen", "power", "wifi")
user_input = input("Type: ")
user_words = user_input.split()

for word in user_words:
     if word in key_words:
          print("you should do this...")

You can use set intersections.

if  set(key_words) & set(user_input.split()):
    print ("you should do this...")

Another Option

This is much easier and self-explanatory. Count each word in key_words. if any
of those just say you should do this...

any_word =  [ True  for x in user_input.split() if x in key_words]

'''
user_input.split() return type is a list
so we should check whether each word in key_words
if so then True
'''


'''
 finally we check the list is empty
'''

if  any_word :
    print ("you should do this...")

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