1

I need to ask a user to input a question that will be compared to a list. The matched word will be displayed and then linked to an option menu. I have added the code below. I have managed to get the the program to search the input and return the word in the find list if a match appears. However I can not figure out how to use the result in an if statement as it is not a string value. I know there is a long way of doing this but is there a simple way of changing 'result' to a string value?

import re
question = input("Please enter your problem:")
find=["display","screen","battery"]
words=re.findall("\w+",question)
result=[x for x in find if x in words]
print (result)
if result in find:
    print("Is your display not working?")
else:
    print("Hard Luck")

Sorry I forgot to say that the outcome of the match will result in a different if statement being selected/printed. For example - If the 'question' used the word 'display' then an IF statement suggesting a solution will be printed, elif the 'question' used the word 'screen' then I elif for a solution to a broken screen will be printed and elif 'question' used 'battery' elif solution to charge the battery will be printed. The problem is I can not change 'result' to a str value to use in an IF statement. I can not check - if result=="display".. or if result=="screen".. or if result=="battery"...

5
  • What are you trying to check? is one of the elements of result is in find ? Sep 11, 2015 at 11:20
  • 3
    ['a'] in ['a'] is going to be False... Sep 11, 2015 at 11:23
  • 2
    if result: should just work.
    – YOU
    Sep 11, 2015 at 11:24
  • what if there are multiple matches? Sep 11, 2015 at 11:40
  • Sorry I forgot to say that the outcome of the match will result in a different if statement being selected/printed. For example - If the 'question' used the word 'display' then an IF statement suggesting a solution will be printed, elif the 'question' used the word 'screen' then I elif for a solution to a broken screen will be printed and elif 'question' used 'battery' elif solution to charge the battery will be printed. The problem is I can not change 'result' to a str value to use in an IF statement. I can not check - if result=="display".. or if result=="screen".. or if result=="battery"...
    – Ben Wilson
    Sep 11, 2015 at 13:12

4 Answers 4

2

If what you are trying to achieve is given the user input, whether any of the words in the user given by the user is also present in find list. Then you should just check whether the result list is empty or not. If the result list is empty, that means that none of the words in find were present in the list words . Example -

if result:
    print("Is your display not working?")
else:
    print("Hard Luck")

Please note, empty lists are considered False-like in boolean context, so you can simply do if result: , as given above.


An easier way for you to do this would be to create sets from start and use set.intersection(). Example -

import re
question = input("Please enter your problem:")
find=set(["display","screen","battery"])
words=re.findall("\w+",question)
result = find.intersection(words)
if result:
    print("Is your display not working?")
else:
    print("Hard Luck")
0

You may use set type for your task:

import re
question = input("Please enter your problem:")
find={"display","screen","battery"}
words=re.findall("\w+",question)
result=set([x for x in find if x in words])
print (result)
if result & find:
    print("Is your display not working?")
else:
    print("Hard Luck")
0

Considering you are already using a regex to extract the words from question and you considering the matched word will be displayed and then linked to an option menu you should compile the regex using the words you want to search for and do a findall with that, you also need to consider the case, if you want Screen and screen to be considered the same word you need to lower the input string:

import re
question = input("Please enter your problem:").lower()
find = ["display","screen","battery"]
r = re.compile(r"\b|\b".join(find))
words = r.findall(question)
if words:
    print(words)
    print("Is your display not working?")

That like your own code and all the answers here will not return a string, it potentially returns multiple strings including the same word repeated multiple times. If you want to use each word you

If you want the first match or only care if any word matches use search which will also cover is there a simple way of changing 'result' to a string value:

r = re.compile(r"|".join(find))
words = r.search(question)
if words:
    print(words.group())

That will give you the first match in sentence for any word in the input sentence.

You also need to use word boundaries if you don't want to match screens etc..

find = ["display","screen","battery"]
r = re.compile(r"|".join([r"\b{}\b".format(s) for s in find]),re.I)

To do it using sets and get the actual word without a regex you can lower, split and rstrip the punctuation from each word in the sentence, then check if each word is in a set of words using next to get the first match:

from string import punctuation
question = input("Please enter your problem:").lower()

find = {"display","screen","battery"}
words = (word.rstrip(punctuation) for word in question.split())
result = next((x for x in words if word in find), None)

if result:
    print(result)
    print("Is your display not working?")
else:
    print("Hard Luck")

Lastly if you did not actually want the words, you should not use intersection, just check if the set find is disjoint of words which will be True if any word in words appears in find:

find = {"display","screen","battery"}
words = re.findall("\w+",question)

if not find.isdisjoint(words):
    print("Is your display not working?")
else:
    print("Hard Luck")

If you want to output a particular message for each word, use a dict with the appropriate string for each word as the value:

import re

question = input("Please enter your problem:").lower()
find = ["display", "screen", "battery"]
r = re.compile(r"\b|\b".join(find))
output = {"screen":"screen message","display":"display message","battery":"battery message"}

word = r.search(question)
if word:
    print(output[word.group()])

Or for multiple words:

output = {"screen":"screen message","display":"display message","battery":"battery message"}
words = r.findall(question)
print("\n".join([output[word] for word in words]))
0
import re


def questioner():

    question = raw_input("Please enter your problem:")

    find = ["display", "screen", "battery"]
    words = re.findall("\w+", question)
    result = [x for x in find if x in words]

    if len(result) > 0:
        print("Is your display not working?")
    else:
        print("Hard Luck")

if __name__ == "__main__":
    questioner()

You just have to check if result list is subset of existing find list!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.