The error means you're doing something like this:
dictionary[key]
... where key
is an "unhashable type". The key to a dictionary must be "hashable" (essentially, something that can't change -- ie: a string, tuple, number, etc.).
In your case, you're trying to create a dictionary (all_questions
) with dictionaries as keys. A simple solution might be to make all_questions
a list rather than a dictionary (notice the use of square brackets rather than curly brackets):
all_questions = [
{
'question': "Mac and Pc share which of the following things:",
'answer1': "They are both expensive",
'answer2': "They are both Touch Screen",
...
},
{
...
},
...
]
For more information on what "hashable type" means, see this question: Hashable, immutable
You seem to be struggling with this problem, I've seen two or three questions all dealing with the same data structure. I'd like to suggest that you rethink your data structure. If you're having difficulty trying to do something with some data, sometimes the solution is to change the data so that it's easier to manipulate.
You want to have a question, a correct answer, and some incorrect answers, so that you can present them on the screen. You also want to be able to randomly remove incorrect answers at some point in time. Correct? To me that means your correct and incorrect answers should be stored as separate entities.
Alternate solution one
You might want to consider putting your incorrect answers in a list, and keeping the correct answer separately. For example:
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",
],
},
...
]
To create a list of all answers for the first question you can do this:
q = all_questions[0]
answers = q["incorrect answers"]
answers.append(q["correct answer"])
random.shuffle(answers)
With the above, answers
is now a list of all possible answers, but in random order.
To remove two random incorrect answers so that you have one correct and two incorrect answers, again in random order, you would do it like this:
answers = random.sample(q["incorrect answers"], 2)
answers.append(q["correct answer"])
random.shuffle(answers)
In either case, when the user picks an answer you can easily compare it to the correct answer:
if user_answer == q["correct answer"]:
print "correct!"
else
print "incorrect"
Alternate solution 2
Note that the above isn't necessarily the best way to do it. There are many alternatives. For example, the questions could be the dictionary keys, and the value could be a list of answers with the correct one always first. For example:
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"
]
}
With that, to remove two random items you start by removing the correct answer and saving it to a variable. You then remove two items at random and combine them with the correct answer.
Data structures are important
The point is, data structures are important. Think about how you need to access the data, and structure the data so that is easy. In your case you need to be able to easily get the correct answer, and you need to be able to easily remove random items from the list of incorrect answers. Define your structure to make that easy.