为什么这段代码只输出else语句呢?

Why does this code only output the else statement?

所以我正在尝试编写一个程序来询问您的问题,在该查询中搜索关键字,然后在找到某些关键字时输出解决方案。

到目前为止,这是我的代码:

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()

但是当我 运行 它时,我输入了我的问题,但是 没有任何输出

编辑:按照建议,我的新代码是这样的。它会考虑文件位置(使用 seek(0))并正确检查 word 是否在关键字列表中:

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()

问题是,现在 运行 是 else 语句,为什么?

首先想到的是您使用的逻辑 OR 不正确,您应该使用:

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

您可以在 Python 中使用 in:

很好地修复它
if word in ("backlight", "display", "cracked", "touchscreen")

修复此问题后,您会注意到在您的情况下使用 a+ 打开文件是不正确的,原因在 Jim 的回答中有说明。将其更改为 r:

with open("user_query.txt", "r") as query

更新: 您的 else 子句有问题,不应再次调用 keyword_searcher。当您输入如下句子时:

Enter your problem:
My Problem is duh duh virus

它将再次调用该函数并且不会检查该行的其余部分。相反,您应该继续阅读 line:

中的下一个单词
# how your else clause should look:
else:
    print ("Please be more specific\n")

现在每个词都将被评估,如果存在正确的关键字,其他子句将评估为 True


您用 a+ 开头,这会将 "file pointer" 放在文件末尾,因此无法读取任何行。如果你想遍历这些行,你应该用 r 打开。

以下文件为例,注意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

在第一种情况下,我们在文件末尾,所以 for line in f 什么也没有。在第二种情况下,我们有所有可用的行,因此遍历它会产生文件中的行。

你可以做的是使用 f.seek(0) 回到开头,然后开始迭代。