I can not solve a problem in python.
the code opens a text file where are the questions.
how to put the correct answer for each question?
cod:
import random
file = open('/home/jonny/questions.txt', 'r')
text = file.read()
separator = '*'
questions = text.split(separator)
print random.choice(questions)
attempt:
resp=raw_input('answer: ')
for question[0] in questions:
if resp=='a':
print 'gratzz'
txt:
*how are you? a) well b) badly c) do not know *What are your plans for today a) nothing b) something c) do not know
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.jsoncontaining:{ "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:
Example output: