這些字典正在殺死我!詢問用戶索引的列表,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
我仍然收到假,假。請注意我正在嘗試從字典中的值中獲取每個字典的索引號(在列表中)。 –