Python 列表弹出

Python list pop

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

这个错误意味着你正在做这样的事情:

dictionary[key]

... 其中 key 是一个 "unhashable type"。字典的键必须是 "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 type" 含义的更多信息,请参阅此问题: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"
    ]
}

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

数据结构很重要

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