我有一个名为all_questions的列表
all_questions={
{
'question': "Mac and Pc share which of the following things:",
'answer1': "They are both expensive",
'answer2': "They are both Touch Screen",
'answer3': "Intel is now inside both computers",
'answer4': "They both have good security",
'correct_answer': 3,
},
{
'question': "Who was the other person that co-founded Microsoft:",
'answer1': "Bill Gates",
'answer2': "Nathan Myhrvold",
'answer3': "Paul Allen",
'answer4': "Richard Rashid",
'correct_answer': 3,
},
{
'question': "When did Windows 10 come out:",
'answer1': "July 29,2015",
'answer2': "July 30,2015",
'answer3': "July 28,2015",
'answer4': "It was not released in July",
'correct_answer': 1,
}
}名单上有问题和答案。我试着做一个提示按钮,删除随机按钮,取出正确的答案。提示按钮的代码是:
def hint_btn():
choices=[choice1,choice2,choice3,choice4]
q = all_questions[current_question]
h = [('correct_answer', 1), ('correct_answer', 2), ('correct_answer', 3), ('correct_answer', 4)]
f = all_questions.popitem()
h = choices.pop(choices.index(f))
false_answer = random.choice(choices)
false_answer2 = random.choice(choices)
if (random.random() > 0.5):
hint1 = choices[q['correct_answer']]
hint1.pack()
hint_btn()
hint2 = choices[false_answer]
hint2.pack_forget()
hint3 = choices[false_answer2]
hint3.pack_forget()
choices.append(h)但是,我说错了:
TypeError: unhashable type: 'dict' for 'correct_answer': 1发布于 2016-01-13 20:16:52
这个错误意味着您正在做这样的事情:
dictionary[key]..。其中key是一个“不可理解的类型”。字典的关键必须是“可理解的”(本质上,一些不能改变的东西--即:字符串、元组、数字等等)。
在您的例子中,您试图创建一个字典(all_questions),其中字典作为键。一个简单的解决方案可能是将all_questions变成列表而不是字典(注意使用方括号而不是花括号):
all_questions = [
{
'question': "Mac and Pc share which of the following things:",
'answer1': "They are both expensive",
'answer2': "They are both Touch Screen",
...
},
{
...
},
...
]有关“哈斯可类型”含义的更多信息,请参见以下问题:Hashable, immutable
您似乎在努力解决这个问题,我已经看到了两个或三个问题,它们都涉及到相同的数据结构。我建议你重新考虑你的数据结构。如果您在试图处理某些数据时遇到困难,有时解决方案是更改数据,以便更容易地操作。
你想要一个问题,一个正确的答案,和一些不正确的答案,这样你就可以在屏幕上呈现它们。您还希望能够在某个时间点随机删除不正确的答案。对,是这样?对我来说,这意味着你的正确和不正确的答案应该存储在单独的实体中。
交替解一
您可能需要考虑将不正确的答案放在列表中,并单独保留正确的答案。例如:
all_questions = [
{
"question": "Mac and Pc share which of the following things:",
"correct answer": "Intel is now inside both computers",
"incorrect answers": [
"They are both expensive",
"They are both Touch Screen",
"They both have good security",
],
},
...
]要为第一个问题创建一个所有答案的列表,您可以这样做:
q = all_questions[0]
answers = q["incorrect answers"]
answers.append(q["correct answer"])
random.shuffle(answers)有了以上所述,answers现在是所有可能的答案的列表,但按随机顺序排列。
要删除两个随机不正确的答案,以便您有一个正确的和两个不正确的答案,同样按随机顺序,您可以这样做:
answers = random.sample(q["incorrect answers"], 2)
answers.append(q["correct answer"])
random.shuffle(answers)在任何一种情况下,当用户选择一个答案时,您可以很容易地将其与正确的答案进行比较:
if user_answer == q["correct answer"]:
print "correct!"
else
print "incorrect"交替解决方案2
请注意,以上并不一定是最好的方法。有很多种选择。例如,问题可以是字典键,值可以是包含正确答案的列表。例如:
all_questions = {
"Mac and Pc share which of the following things": [
"They both have intel inside",
"They are both expensive",
"They are both Touch Screen",
"They both have good security"
]
}这样,要删除两个随机项,首先删除正确的答案并将其保存到一个变量中。然后随机删除两个项目,并将它们与正确的答案结合起来。
数据结构很重要
关键是,数据结构很重要。考虑一下您需要如何访问数据,并对数据进行结构化,这样就很容易了。在这种情况下,您需要能够轻松地获得正确的答案,并且需要能够轻松地从不正确的答案列表中删除随机项。定义你的结构,使之变得简单。
https://stackoverflow.com/questions/34774924
复制相似问题