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 hel
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')))
def main():
"""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.
break
else:
print('Sorry, but I cannot help you.')
def get_response(query):
"""Ask the user yes/no style questions and return the results."""
while True:
answer = input(query).casefold()
if answer:
if any(option.startswith(answer) for option in POSITIVE):
return True
if any(option.startswith(answer) for option in NEGATIVE):
return False
print('Please provide a positive or negative answer.')
if __name__ == '__main__':
main()
You are looking for an enum
from enum import Enum
class Status(Enum):
NOT_ON = 1
FREEZING = 2
CRACKED = 3
WATER = 4
Then in your if
checks, you do something like this:
if problemselect == Status.NOT_ON:
...
elif problemselect == Status.FREEZING:
...