kesh
kesh

Reputation: 21

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?

Upvotes: 1

Views: 8930

Answers (5)

Saiful Azad
Saiful Azad

Reputation: 1901

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

Upvotes: -1

YBathia
YBathia

Reputation: 902

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

Upvotes: 1

idjaw
idjaw

Reputation: 26560

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.

Upvotes: 3

Seekheart
Seekheart

Reputation: 1173

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

Upvotes: -1

Sci Prog
Sci Prog

Reputation: 2691

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.

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

Upvotes: 1

Related Questions