如何将用户输入与我的文本文件中的关键字进行匹配,并提供输出

时间:2017-05-03 09:21:42

标签: 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:
            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'

我觉得这种情况正在发生,因为我正在使用dictonary。虽然我知道这是rerason,但idont知道如何修复它。有人可以帮我。 这是文件以防万一你需要它:

screen Screen SCREEN display Display DISPLAY : 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.

battery Battery BATTERY power Power POWER : if your phone is out of charge and you cant seem to charge it then you will have to replace either your battery or charger.

audio Audio AUDIO sound Sound SOUND : to fix you sound you can go to settings, then display. there you can change the setting of your devices sound

我也是python的新手。所以你必须忍受我的解释

3 个答案:

答案 0 :(得分:0)

您直接从用户输入中调用密钥。您需要在尝试调用密钥之前搜索关键字,或者您可以输入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])

答案 1 :(得分:0)

问题是你的词典有一个'screen'条目(或'**screen**',我不确定你的代码判断),但不是完整的句子。

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

这是一个简单的演示,应该得到重点。

>>> 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_input代替input。)

答案 2 :(得分:0)

我猜你的记录文件如下: question1:answer1 question2:answer2 ... question_n:answer_n 错误可能在于:

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只将这些单词存储为键。

例如: how old are you:16

在你的代码中,datadict就像: { 'how': 16, 'old': 16, 'are': 16, 'you': 16 } 这不是你想要的