jeudi 21 janvier 2016

How do I count up certain words (not just one word) in a string and then output different code if there are different amounts of words?

Sorry for lengthy question, I didn't know how to word it specifically without it being marked as a duplicate. Anyway, I am trying to count up a set of keywords that a user has inputted which is written into a text file. Here is the code:

import os
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"):
                with open("Solutions1.txt", "r") as f:
                    solution_one  = f.read()
                    print ("Keyword for screen problem found:\n")
                    print (solution_one)
            elif word in ("battery", "charger", "usb", "charge"):
                with open("Solutions2.txt", "r") as f_two:
                    solution_two = f_two.read()
                    print ("Keyword for battery problem found:\n")
                    print(solution_two)
            elif word in ("virus", "hacked", "infected", "antivirus"):
                with open("Solutions3.txt", "r") as f_three:
                    solution_three = f_three.read()
                    print ("Keyword for virus problem found:\n")
                    print (solution_three)
            else:
                pass
keyword_searcher()
os.remove("user_query.txt")

My problem is, when the user inputs more than one of any keywords, it outputs the code that amount of times, when I only want it once. How do I fix this?

Aucun commentaire:

Enregistrer un commentaire