当用户输入短语时,短语中的关键字需要与文本文件中的文本匹配,然后可以将文本文件中的行打印回给用户。
例如,当用户键入“我的电话屏幕为空白”或“显示屏为空白”时,应该有相同的解决方案从文本文件输出到屏幕。
searchfile = open("phone.txt", "r")
question = input (" Welcome to the phone help center, What is the problem?")
if question in ["screen", "display", "blank"]:
for line in searchfile:
if question in line:
print (line)
elif question in ["battery", "charged", "charging", "switched", "off"]:
for line in searchfile:
if question in line:
print (line)
else:
if question in ["signal", "wifi", "connection"]:
for line in searchfile:
if question in line:
print (line)
searchfile.close()
在文本文件中:
屏幕:屏幕需要更换电池:电池需要充电信号:您没有信号
首先,以下两行无法按照您的要求运行:
question = raw_input(" Welcome to the phone help center, What is the problem?")
if question in ["screen", "display", "blank"]:
如果用户键入我的电话屏幕为空白,因为完整句子不是列表的成员,则其余if不会执行。相反,您应该测试句子中是否存在列表的任何成员:
question = raw_input(" Welcome to the phone help center, What is the problem?")
for k in ["screen", "display", "blank"]:
if k in question:
for line in searchfile:
if k in line: # or maybe if 'screen' in line ?
print line
break
break
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句