从文本文件中识别关键词

Identifying key words from a text file

嘿,我有一个很简单的问题,我在任何地方都找不到。基本上我需要知道如何让用户输入一个问题,然后从该输入中识别关键字。现在这些关键字将存储在几个文本文件中,并且必须打开相关的文本文件,例如关键词 water 打开 water 文本文件,然后向用户呈现一系列是或否的问题,之后最终会产生两种结果。

我确实到处查看如何执行此操作,但所有代码看起来都不一样或者不是我要找的代码。对代码的任何方面的任何帮助将不胜感激。

#Task 2 Trouble-Shooter 1.0
#Zacharriah River Howells
file = open('problem1','r')
import time
print('Hello and welcome to the mobile phone Trouble-Shooter!')
time.sleep(2)
print('Please input what is wrong with your phone')

print file.read()

这是我目前所拥有的,直到最后一行。

试试这个:

import time

print('Hello and welcome to the mobile phone Trouble-Shooter!')
time.sleep(2)

need = raw_input('Please input what is wrong with your phone ')
file = open(need,'r')
print file.read()

raw_input 函数内置于 Python 2 中,它 returns 是一个字符串。接下来打开具有输入名称的文件并打印其内容。