【发布时间】:2016-07-13 14:19:49
【问题描述】:
我可以成功导入字典,并且可以从字典中获取其值的输出,但它会显示所有值,而不是与用户输入匹配的值。
输入首先被转换为低级,然后拆分成单个单词以在字典中引用。
# prob_dict : dictionary
# problemlist : input lowercase and split
我搜索了很多帖子,但找不到有效的解决方案。
problemlist = problem1.split()
for problem in range(len(prob_dict)):
if prob_dict in problemlist:
solution = []
solution = (prob_dict[problem])
print('Your Solution is:', solution)
else:
print('could not find a solution')
字典是:
prob_dict = {'wet': ['put in bag of rice to dry out'],
'screen': ['screen will need to be replaced'],
'charger': ['purchase new charger for our store']
}
【问题讨论】:
-
和
problem1是什么? -
嗨,我不明白你的问题。能详细点吗?
-
请提供 MCVE(请参阅 How to create a Minimal, Complete, and Verifiable example)。
-
好的,所以代码问了 3 个问题,第三个问题是一个输入,询问您的设备有什么问题(问题 1 是那个输入)然后我将它设置为将所有文本从输入转换为小写并将其拆分,以便我可以将输入中的单词与字典中的键进行比较并生成答案示例:用户输入我弄湿了。然后程序从句子中查找与字典中的键匹配的单词,即单词“wet”并输出键值。
标签: python dictionary input