So I'm trying to write a program that will ask for your problem, search for a keyword in that query and then output a solution if certain keywords are found.
This is my code so far:
def keyword_searcher():
user_query = input("Enter your problem:\n")
with open("user_query.txt", "a+") as query:
query.write(user_query.lower())
for line in query:
for word in line.split():
if word == "backlight" or "display" or "cracked" or "touchscreen":
f = open("Solutions1.txt", "r")
solution_one = f.read()
print ("Keyword for screen problem found:\n")
print (solution_one)
f.close()
elif word == "battery" or "charger" or "usb" or "charge":
f_two = open("Solutions2.txt", "r")
solution_two = f_two.read()
print ("Keyword for battery problem found:\n")
print(solution_two)
f.close()
elif word == "virus" or "hacked" or "infected" or "antivirus":
f_three = open("Solutions3.txt", "r")
solution_three = f_three.read()
print ("Keyword for virus problem found:\n")
print (solution_three)
else:
print ("Please be more specific\n")
keyword_searcher()
But when I run it, I input my problem but then nothing gets outputted.
EDIT: As suggested, my new code is this. It takes into account the file position (with seek(0)
) and checks correctly if a word
is in a list of keywords:
def keyword_searcher():
user_query = input("Enter your problem:\n")
with open("user_query.txt", "a+") as query:
query.write(user_query.lower())
query.seek(0)
for line in query:
for word in line.split():
if word in ("backlight", "display", "cracked", "touchscreen"):
f = open("Solutions1.txt", "r")
solution_one = f.read()
print ("Keyword for screen problem found:\n")
print (solution_one)
f.close()
elif word in ("battery", "charger", "usb", "charge"):
f_two = open("Solutions2.txt", "r")
solution_two = f_two.read()
print ("Keyword for battery problem found:\n")
print(solution_two)
f.close()
elif word in ("virus", "hacked", "infected", "antivirus"):
f_three = open("Solutions3.txt", "r")
solution_three = f_three.read()
print ("Keyword for virus problem found:\n")
print (solution_three)
else:
print ("Please be more specific\n")
keyword_searcher()
Problem is, it now runs the else
statement, why?