I am trying to create a multiple choice question program that will read a text file, pose 5 of the questions in a random order, receive the answer, and then display the answer if it's false. Here's what I've managed to write:
First an example of the file containing the questions:
mcq.txt:
Which of these countries is an island ? A-France B-Lithuania C-Australia D-Korea;;C
One byte is A-1bit B-4 bits C-8 bits D-32 bits;;C
What event of 1934 contributed to the creation of the Popular Front? A-The congress of Tours B-The riots of the leagues C-The Crash of Wall-Street;;B
What event is at the origin of the crisis of the Thirties? A-The Russian Revolution B-The rise of fascism C-The First World War D-The Wall Street Crash;;D
What is the color of Adam's white horse A-Green B-Red C-Grey D-White;;D
With 1 byte you can encode A-255 characters B-8 characters C-16 characters D-256 characters;;D
What is Microsoft Windows? A-A type of computer B-An operating system C-A database management system;;B
The binary system uses the base A-2 B-10 C-8 D-16 E-None of these answers are true.;;A
In Greco-Roman mythology, the gods met on A-Mount Sinai B-Vesuvius C-Mount Olympus;;C
In Greek mythology, Pegasus is A-A winged horse B-A man with an eagle's head C-A giant;;A
When was the World War II armistice signed A-May 8, 1945 B-July 14, 1940 C-November 11, 1945 D-June 18, 1940;;A
Before 2003, the countries of Bosnia, Croatia, Slovenia, Macedonia, Montenegro and Serbia formed A-Bulgaria B-Czechoslovakia C-Kosovo D-Yugoslavia;;D
What is this infectious disease caused by Koch's bacillus called? A-Plague B-Tuberculosis C-Poliomyelitis D-Scabies;;B
the program:
import random
def mcq():
with open("mcq.txt", "r") as f:
i=0
while i<5:
line=f.readline()
a=line.split(";;")
print(a[0])
answer=input("your answer?")
if answer.upper()==a[1]:
print("correct")
else:
print("false, the anwer was", a[1])
i=i+1
mcq()
The program seems to be working but asks the questions in order, which is normal because we didn't use a random module, but when we put random.shuffle(line)
we encounter the error "'str' object does not support item assignment".
To solve this I tried writing this program which is supposed to assign numbers to each line in a roundabout way and ask the questions in a random order, but it just gives numbers and is pretty much useless:
import random
with open("qcm.txt", "r") as f:
ligne=f.read().splitlines()
i=range(0,30)
x=zip(ligne,i)
for i in range(10):
x=random.randint(0, 30)
print(x)
The other problem with the main program is that the answer is always false. Even though I put upper()
it doesn't seem to change anything.
I also thought about using dictionaries and assigning the question to the key and the answer to the value, but I don't know how to randomise it and if it's a good idea.
I would be grateful if you could help me.
Aucun commentaire:
Enregistrer un commentaire