这些字典正在杀死我!询问用户索引的列表,Python
这是我的JSON(问题)包含2个字典的列表。
[
{
"wrong3": "Nope, also wrong",
"question": "Example Question 1",
"wrong1": "Incorrect answer",
"wrong2": "Another wrong one",
"answer": "Correct answer"
},
{
"wrong3": "0",
"question": "How many good Matrix movies are there?",
"wrong1": "2",
"wrong2": "3",
"answer": "1"
}
]
我想让用户搜索一个索引,然后列出该索引的值(如果有的话)。目前,我提示用户输入,然后查找问题索引,然后检查输入是否等于索引,现在即使输入了正确的索引,它也会返回False,False。我在正确的轨道上,但语义错误?或者我应该研究另一条路线?
import json
f = open('question.txt', 'r')
questions = json.load(f)
f.close()
value = inputSomething('Enter Index number: ')
for i in questions:
if value == i:
print("True")
else:
print("False")
我得到一个NameError,我没有定义? –
看到我更新的答案.. – Zealous
我仍然收到假,假。请注意我正在尝试从字典中的值中获取每个字典的索引号(在列表中)。 –