Python列表弹出

标签 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

最佳答案

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

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"
    ]
}

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

数据结构很重要

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

关于Python列表弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34774924/

相关文章:

python - 将 float 分隔成数字

python - 如何使顶层以外的区域不可点击?

python - 删除字符串中的字符 "' s "

pip - 在python3.2中安装pip<v8

python - 使用带有日期/时间的 CSV 文件的 loadtext

python - 为什么返回自身的函数在 python 3 中最大递归

python - Tkinter——如何水平居中 Canvas 文本?

Python tkinter列表框获取索引

python - 在 fileinput 模块中结合就地过滤和编码设置

python - 为什么 Python 2.7 命令行工具不位于 Mac OS X 上的 `/usr/local/bin` 中?