1

Right now i have a list of each separate key word is there a way to make this into one array or string and search for individual words instead of having to separate each key word and make it repeatable?

keywordlist = ("pricing")
keywordlist1 = ("careers")
keywordlist2 = ("quotes")
keywordlist3 = ("telephone number")
keywordlist5 = ("about")
while True:
    question1 = raw_input("Is there anything I can help with today?   ")
    question_parts = question1.split(" ")
    for word in question_parts:
        if word.lower() in keywordlist:
            print("Yah i can show you some pricing:   ")
        elif word.lower() in keywordlist1:
            print("Here is our career page and feel free to apply:  ")
        elif word.lower() in keywordlist2:
            print("yah i can show you to our quote page: ")
        elif word.lower() in keywordlist3:
            print("Yah here is our contact page and feel free to contact us: ")
        elif word.lower() in keywordlist5:
            print("Here is some information about us:")
        goagain = raw_input("Would you like any More Help? (Yes/No)")
        if goagain == "Yes":
            #Get values again
            pass #or do whatever
        elif goagain != "Yes":
            print ("Bye!")
    break
1
  • Your keywordlist etc aren't lists or tuples, they're just strings.
    – PM 2Ring
    Commented Nov 4, 2017 at 3:51

2 Answers 2

3

You can use a dictionary

keywords = {"pricing": "Yah i can show you some pricing:   "
            "careers": "Here is our career page and feel free to apply:  "
            "quotes": "yah i can show you to our quote page: "
            "telephone": "Yah here is our contact page and feel free to contact us: "
            "about": "Here is some information about us:"}
while True:
    question1 = raw_input("Is there anything I can help with today?   ")
    question_parts = question1.lower().split()
    for word in question_parts:
        if word in keywords:
            print(keywords[word])
    goagain = raw_input("Would you like any More Help? (Yes/No)")
    if goagain == "Yes":
        #Get values again
    else:
        print ("Bye!")
        break
3
  • Nice work. FWIW, you could change that .split call to question1.split() to make it split on all whitespace.
    – PM 2Ring
    Commented Nov 4, 2017 at 4:03
  • @PM2Ring Done! And also convert to lowercase the whole input string in order to avoid converting it each time. Thank you! Commented Nov 4, 2017 at 4:08
  • Good idea. I forgot to mention that, although I did it in my own answer, (which I won't bother posting now that you've posted yours). Converting the case of a string is pretty cheap, but there's no point in doing it over & over on the same string.
    – PM 2Ring
    Commented Nov 4, 2017 at 4:13
0

use regular expressions

import re 
KEYWORD_MAPPING = {
        "pricing": "Yah i can show you some pricing:",
        "careers": "Here is our career page and feel free to apply:",
        "quotes": "yah i can show you to our quote page:",
        "telephone": "Yah here is our contact page and feel free to contact us: ",
        "about": "Here is some information about us:"
}

def search_for_keyword(question):
    keyword_pattern = r'{}'.format('|'.join(KEYWORD_MAPPING.keys()))
    keyword = re.search(keyword_pattern, question, re.I).group()
    if not keyword:
         return False
    print(KEYWORD_MAPPING[keyword])
    return True

while True:
        question = input('enter a question')
        status = search_for_keyword(question)
        if not status:
             break

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.