-3

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?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253

2 Answers2

1

First thing that comes to mind is that you are using the logical OR incorrectly, you should use:

if word == "backlight" or word == "display" or word == "cracked" or word == "touchscreen"

You can fix it nicely in Python by using in:

if word in ("backlight", "display", "cracked", "touchscreen")

After you fix this, you would want to notice that opening your file with a+ is incorrect in your case, for the reason stated in Jim's answer. Change it to r:

with open("user_query.txt", "r") as query
Idos
  • 15,053
  • 14
  • 60
  • 75
1

Update: your else clause is problematic, it shouldn't call keyword_searcher again. When you enter a sentence like:

Enter your problem:
My Problem is duh duh virus

It will call the function again and won't check the rest of the line. Instead, you should simply continue on to the next word in the line:

# how your else clause should look:
else:
    print ("Please be more specific\n")

Now everyword will be evaluated and the other clauses will evaluate to True if a correct keyword is present.


Your opening with a+ this places the "file pointer" to the end of the file so no lines can be read. You should instead open with r if you want to iterate through the lines.

The the following file for example and notice the differences in file.tell():

>>> with open("jupyter_notebook_config.py", "a+") as f:
...    print(f.tell())
>>> 22821

>>> with open("jupyter_notebook_config.py", "r") as f:
...    print(f.tell())
>>> 0

In the first case we're at the end of the file so for line in f just doesn't have anything. In the second case we have all lines available so iterating through it yields the lines in the file.

What you could do is use f.seek(0) to go to the beginning and then start iterating.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • I opened it with a+ because i thought it would allow me to read and write - guess i was wrong. But what would i use to read and write in a text file because opening with r is read only mode. – Didgeri Duck Jan 17 '16 at 11:58
  • I have now changed it to seek to 0 and fixed the incorrect use of OR but now whenever i input a keyword (e.g. "cracked") it just outputs the else statement... – Didgeri Duck Jan 17 '16 at 12:08
  • Can someone help these are only partial solutions to a bigger problem – Didgeri Duck Jan 17 '16 at 13:13
  • What exactly are you entering when it prompts you for some text @DidgeriDuck? – Dimitris Fasarakis Hilliard Jan 17 '16 at 13:23
  • @Jim I'm just inputting a sentence with one of the keywords in it e.g. "My phone has a virus" ... virus being the keyword – Didgeri Duck Jan 17 '16 at 13:53
  • Also, this now spams the else statement and then at the end of the output it recognises the keyword... what am i doing wrong? – Didgeri Duck Jan 17 '16 at 13:54
  • You have a `print` in the `else`, remove it. It searches for **each word in the line sequentially**. If the matching keyword is at the end it is going to get matched in the end. – Dimitris Fasarakis Hilliard Jan 17 '16 at 13:56
  • What would i put in the else statement, because at the moment the code just outputs nothing even if there is a keyword in the query :| – Didgeri Duck Jan 17 '16 at 14:03
  • I wouldn't put anything in it. Additionally we've maxed out the comments here, I really can't check whether it outputs something or not since I don't have the files you're using nor the time to set it up. – Dimitris Fasarakis Hilliard Jan 17 '16 at 14:09