Python 如何将用户输入与文本文件中的关键字匹配,并给出输出

Python 如何将用户输入与文本文件中的关键字匹配,并给出输出,python,input,match,Python,Input,Match,我需要向用户请求输入(您有什么问题?) 我创建了一个文件,其中包含可能存在的问题以及这些问题的可能解决方案。如果用户输入的关键字与文件中的关键字匹配,程序应该打印出解决方案。代码如下: print ("welcome to our automated trouble shooting system") def do_again(): datadict = {} with open('prosol.txt') as file: for rec in file:

我需要向用户请求输入(您有什么问题?) 我创建了一个文件,其中包含可能存在的问题以及这些问题的可能解决方案。如果用户输入的关键字与文件中的关键字匹配,程序应该打印出解决方案。代码如下:

print ("welcome to our automated trouble shooting system")

def do_again():
    datadict = {}
    with open('prosol.txt') as file: 
        for rec in file:
            rec = rec.split(':')
            problem = rec[0]
            answer = rec[1] 
            problem = problem.split(" ")
            for item in problem: 
                datadict[item] = answer



    user_problem = input('What is the problem?: ') 
    print(datadict[user_problem]) 


    repeat = input("do you have any other problems.\n1. Yes\n2. No\n") 
    try_again = ["1","2"] 
    while repeat not in try_again: 
        repeat = input("do you have any other problems.(please answer using the corrinsponding numbers)\n1. Yes\n2. No\n") 
    if repeat == "1": 
        (do_again()) 
    elif repeat == "2": 
        print("bye. i hope it helped you") 
        quit() 

(do_again()) 
当我使用一个单词关键字时,它就起作用了。比如说

welcome to our automated trouble shooting system
What is the problem?: **screen**
 if your screen is broken, then you will have to replace it. if it is frozen the try turning off your phone and the turn it on again.

do you have any other problems.
1. Yes
2. No
welcome to our automated trouble shooting system
What is the problem?: **my screen doesnt work**
Traceback (most recent call last):
  File "C:\Users\hp\Downloads\task2\mine\task 2.py", line 38, in <module>
(do_again()) ##calling the function


File "C:\Users\hp\Downloads\task2\mine\task 2.py", line 18, in do_again
    print(datadict[user_problem]) ##print the value of the kye selected

KeyError: 'my screen doesnt work'
但是如果我写一个完整的句子,它是不起作用的

welcome to our automated trouble shooting system
What is the problem?: **screen**
 if your screen is broken, then you will have to replace it. if it is frozen the try turning off your phone and the turn it on again.

do you have any other problems.
1. Yes
2. No
welcome to our automated trouble shooting system
What is the problem?: **my screen doesnt work**
Traceback (most recent call last):
  File "C:\Users\hp\Downloads\task2\mine\task 2.py", line 38, in <module>
(do_again()) ##calling the function


File "C:\Users\hp\Downloads\task2\mine\task 2.py", line 18, in do_again
    print(datadict[user_problem]) ##print the value of the kye selected

KeyError: 'my screen doesnt work'

我也是python的新手。因此,您必须接受我的解释

您直接从用户输入调用密钥。您需要在尝试调用键之前搜索关键字,或者可以在try-except子句中输入一个关键字,以捕获它们调用的键不在字典中的异常

user_answer = input("What's your problem?")
split_answer = user_answer.split(" ")
for option in datadict.keys():
    if option in split_answer:
        print(datadict[option])

您直接从用户输入调用密钥。您需要在尝试调用键之前搜索关键字,或者可以在try-except子句中输入一个关键字,以捕获它们调用的键不在字典中的异常

user_answer = input("What's your problem?")
split_answer = user_answer.split(" ")
for option in datadict.keys():
    if option in split_answer:
        print(datadict[option])

问题是,您的词典中有一个条目用于
'screen'
(或
'**screen**'
,我不确定是否根据您的代码判断),但不用于任何完整的句子

您需要首先识别表明用户输入中存在问题的关键字,然后打印字典存储的解决方案

user_answer = input("What's your problem?")
split_answer = user_answer.split(" ")
for option in datadict.keys():
    if option in split_answer:
        print(datadict[option])
下面是一个简单的演示,应该能让大家明白这一点

>>> answers = {'screen': 'do A', 'water': 'do B', 'power': 'do C'}
>>> user_input = input('what is the problem? ')
what is the problem? my screen is damaged from a water spill
>>> words = user_input.lower().split()
>>> for problem in words:
...     if problem in answers:
...         print(answers[problem])
... 
do A
do B

(我假设您使用的是Python3,如果您使用的是Python2,请使用
raw_input
而不是
input

问题是,您的词典中有一个条目用于
'screen'
(或
'**screen**
,我不确定是否根据您的代码来判断),但不用于任何完整的句子

您需要首先识别表明用户输入中存在问题的关键字,然后打印字典存储的解决方案

user_answer = input("What's your problem?")
split_answer = user_answer.split(" ")
for option in datadict.keys():
    if option in split_answer:
        print(datadict[option])
下面是一个简单的演示,应该能让大家明白这一点

>>> answers = {'screen': 'do A', 'water': 'do B', 'power': 'do C'}
>>> user_input = input('what is the problem? ')
what is the problem? my screen is damaged from a water spill
>>> words = user_input.lower().split()
>>> for problem in words:
...     if problem in answers:
...         print(answers[problem])
... 
do A
do B

(我假设您使用的是Python 3,如果您使用的是Python 2,请使用
raw\u input
而不是
input

我猜您的记录文件看起来像:

问题1:答案1
问题2:答案2
...
问题:答案
错误可能就在这里:

def do_again():
    datadict = {}
    with open('prosol.txt') as file: 
        for rec in file:
            rec = rec.split(':')
            problem = rec[0]
            answer = rec[1] 
            problem = problem.split(" ")
            for item in problem: 
                datadict[item] = answer
problem=problem.split(“”)
只需将整个句子拆分为单词,而
datadict
只将这些单词存储为键

例如:

你多大了:16岁

在您的代码中,datadict类似于:

{
“如何”:16,
“老”:16岁,
"是":16,,
“你”:16岁
}

这不是您想要的

我猜您的记录文件看起来像:

问题1:答案1
问题2:答案2
...
问题:答案
错误可能就在这里:

def do_again():
    datadict = {}
    with open('prosol.txt') as file: 
        for rec in file:
            rec = rec.split(':')
            problem = rec[0]
            answer = rec[1] 
            problem = problem.split(" ")
            for item in problem: 
                datadict[item] = answer
problem=problem.split(“”)
只需将整个句子拆分为单词,而
datadict
只将这些单词存储为键

例如:

你多大了:16岁

在您的代码中,datadict类似于:

{
“如何”:16,
“老”:16岁,
"是":16,,
“你”:16岁
}

这不是您想要的

快速提示:与其检查单个单词的许多不同大小写变体,不如只比较所有小写输入。e、 g.:
if-input(“>”).lower()==“screen”
快速提示:不要检查单个单词的许多不同大小写变体,只需比较所有小写输入即可。e、 g.:
如果输入(“>”).lower()==“screen”
我如何搜索一个键?那么假设用户输入“我的电脑屏幕坏了”,如果您进行用户输入查询。拆分(“”)并在拆分列表中搜索关键字,您可以根据答案调用答案。我将在我的帖子中添加一个编辑,2秒钟,我如何搜索一个关键字?假设用户输入“我的电脑屏幕坏了”,如果你进行用户输入查询。拆分(“”),然后在拆分列表中搜索你的关键字,你可以根据他们的答案调用答案。我会在我的帖子中添加编辑,2秒钟