Welcome to DaniWeb and the interesting world of Python coding!
The Quiz.dat file is just a text file containing 6 lines per question. The first line is the correct answer code for the four possible answers (A, B, C, D), followed by the question and the four possible answers. For example ...
A
Which sport uses the term LOVE ?
Tennis
Golf
Football
Swimming
B
What is the German word for WATER ?
Wodar
Wasser
Werkip
Waski
C
What has eight bits ?
Word
ASCII
Byte
RAM
...
...
To work with it in Python, it is easiest to form a tuple/array of each question in a set order and put them in a list. I picked the order (question, answer1, answer2, answer3, answer4, answer_code). The reason I used a list of tuples is that you can shuffle them easily. You read the data file into your program as a list of text lines and then convert these to a list of the question tuples ...
[('Which sport uses the term LOVE ?', 'Tennis', 'Golf', 'Football', 'Swimming', 'A'),
('What is the German word for WATER ?', 'Wodar', 'Wasser', 'Werkip', 'Waski', 'B'),
('What has eight bits ?', 'Word', 'ASCII', 'Byte', 'RAM', 'C'),
...]
Here is a short Python program to handle the formation of the list of tuples ...
# read the mutliple choice quiz data file
# reorganize into a list of tuples, each tuple has this order
# (question, answer1, answer2, answer3, answer4, answer_code)
# read the appended text file as a list of lines
fin = open("Quiz.dat", "r")
lineList = fin.readlines()
fin.close()
quiz_list = []
for k, line in enumerate(lineList):
tuple_complete = False
line1 = line.strip()
# build a tuple for each question
if k % 6 == 0:
answer_code = line1
if answer_code == 'end':
break
if k % 6 == 1:
question = line1
if k % 6 == 2:
answer1 = line1
if k % 6 == 3:
answer2 = line1
if k % 6 == 4:
answer3 = line1
if k % 6 == 5:
answer4 = line1
tuple_complete = True
if tuple_complete:
tuple1 = (question, answer1, answer2, answer3, answer4, answer_code)
print tuple1 # test
quiz_list.append(tuple1)
print "-"*70
#print quiz_list # for testing
print "-"*70
# shuffle the list and display the first 10 question tuples
import random
random.shuffle(quiz_list)
for k, quest in enumerate(quiz_list):
if k < 10:
print quest
Now its up to you to display the question, followed by the four possible answers titled A, B, C, D and ask the player to give the title letter of the correct answer. After the user gives the input, then you compare the letter with the letter of the correct answer.