Try storing your data in a standard structured format such as JSON. A library for parsing JSON is included with Python.
Create a file called questions.json
containing:
{ "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 } }
Then you can parse it and ask the user a random question like this:
import json
import random
# read data from JSON file
questions = json.load(open("questions.json"))
# pick a random question
question = random.choice(questions.keys())
# get the list of possible answers and correct answer for this question
answers = questions[question]['answers']
correct_answer = questions[question]['correct_answer']
# display question and list of possible answers
print question
for n, answer in enumerate(answers):
print "%d) %s" % (n + 1, answer)
# ask user for answer and check if it's the correct answer for this question
resp = raw_input('answer: ')
if resp == str(correct_answer):
print "correct!"
else:
print "sorry, the correct answer was %s" % correct_answer
Example output:
What are your plans for today? 1) nothing 2) something 3) do not know answer: 3 sorry, the correct answer was 2
5
solved create random questions using a txt file