當用戶輸入一個詞組時,短語中的關鍵詞需要與文本文件中的文本匹配,然後將該行輸出文本文件可以打印回給用戶。在文本文件中搜索與用戶輸入的文本匹配的文本行Python
例如當用戶輸入「我的手機屏幕爲空白」或「顯示屏幕爲空白」時,應從文本文件中輸出相同的解決方案。
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()
的文本文件中:
屏幕:您的屏幕需要更換電池:你的電池需要充電信號:你沒有信號
可以分享phone.txt文件嗎?如果不是保密的話。 –