Python列表pop

时间:2016-01-13 19:01:22

标签: python tkinter python-3.2

我有一个名为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

1 个答案:

答案 0 :(得分:3)

错误意味着您正在执行以下操作:

dictionary[key]

...其中key是"不可用的类型"。字典的关键必须是" hashable" (基本上,一些无法改变的东西 - 即:字符串,元组,数字等)。

在您的情况下,您尝试使用词典作为键创建字典(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类型"的更多信息。意思是,请看这个问题: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"
    ]
}

这样,要删除两个随机项,首先要删除正确答案并将其保存到变量中。然后随机删除两个项目并将它们与正确的答案结合起来。

数据结构很重要

关键是,数据结构很重要。考虑一下如何访问数据,以及如何轻松构建数据。在您的情况下,您需要能够轻松获得正确答案,并且您需要能够轻松地从错误答案列表中删除随机项目。定义您的结构以使其变得简单。