Skip to content Skip to sidebar Skip to footer

How Can I Put Keywords Into My Code?

Okay so how could I make my code so instead of putting 1 to 4 I could just put in a keyword like 'Freezing' or 'Froze' How could i put a keyword searcher in my code, all help is im

Solution 1:

Since you want the user to type in the description of the phone's problem, you probably want a list of problems and keywords associated with those problems. The following program shows how you might arrange such a database and how to search it based on the user's input. There are faster alternatives to this solution (such as indexing the database), but this basic implementation should be sufficient for the time being. You will need to provide further code and information on how to resolve problems, but answers to your previous question should help direct you in that regard.

#! /usr/bin/env python3# The following is a database of problem and keywords for those problems.# Its format should be extended to take into account possible solutions.
PROBLEMS = (('My phone does not turn on.',
             {'power', 'turn', 'on', 'off'}),
            ('My phone is freezing.',
             {'freeze', 'freezing'}),
            ('The screen is cracked.',
             {'cracked', 'crack', 'broke', 'broken', 'screen'}),
            ('I dropped my phone in water.',
             {'water', 'drop', 'dropped'}))


# These are possible answers accepted for yes/no style questions.
POSITIVE = tuple(map(str.casefold, ('yes', 'true', '1')))
NEGATIVE = tuple(map(str.casefold, ('no', 'false', '0')))


defmain():
    """Find out what problem is being experienced and provide a solution."""
    description = input('Please describe the problem with your phone: ')
    words = {''.join(filter(str.isalpha, word))
             for word in description.lower().split()}
    for problem, keywords in PROBLEMS:
        if words & keywords:
            print('This may be what you are experiencing:')
            print(problem)
            if get_response('Does this match your problem? '):
                print('The solution to your problem is ...')
                # Provide code that shows how to fix the problem.breakelse:
        print('Sorry, but I cannot help you.')


defget_response(query):
    """Ask the user yes/no style questions and return the results."""whileTrue:
        answer = input(query).casefold()
        if answer:
            ifany(option.startswith(answer) for option in POSITIVE):
                returnTrueifany(option.startswith(answer) for option in NEGATIVE):
                returnFalseprint('Please provide a positive or negative answer.')


if __name__ == '__main__':
    main()

Solution 2:

You are looking for an enum

from enumimport Enum
classStatus(Enum):
    NOT_ON =1
    FREEZING = 2
    CRACKED = 3
    WATER = 4

Then in your if checks, you do something like this:

ifproblemselect== Status.NOT_ON:
    ...
elifproblemselect== Status.FREEZING:
    ...

Post a Comment for "How Can I Put Keywords Into My Code?"