コード:
import json
import random
questions = json.load(open("questions.json"))
question = random.choice(questions.keys())
answers = questions[question]['answers']
correct_answer = questions[question]['correct_answer']
print question
for n, answer in enumerate(answers):
print "%d) %s" % (n + 1, answer)
resp = raw_input('answer: ')
if resp == str(correct_answer):
print "correct!"
else:
print "sorry, the correct answer was %s" % correct_answer
質問.json:
{
"What are your plans for today?": {
"answers": ["nothing", "something", "do not know"],
"correct_answer": 2
},
"how are you?": {
"answers": ["well", "badly", "do not know"],
"correct_answer": 1
}
}
このプログラムで、質問を止めずに質問を続けるように、答えが正しくても間違っていても、質問を続けられるようにするにはどうすればよいか知りたいです。