如何使用外部文本文件创建多选题程序



我正在尝试创建一个多选题程序,该程序将读取一个文本文件,按随机顺序提出其中5个问题,接收答案,如果答案为假,则显示答案。以下是我设法写的:

首先是一个包含问题的文件示例:

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

程序:

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()

程序似乎在工作,但按顺序提问,这很正常,因为我们没有使用随机模块,但当我们放置random.shuffle(line)时,我们会遇到错误"str‘object不支持项分配";。

为了解决这个问题,我尝试编写这个程序,它应该以迂回的方式为每一行分配数字,并以随机顺序提问,但它只是给出数字,几乎没有用:

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)

主程序的另一个问题是答案总是错误的。即使我放了upper(),它似乎也没有改变任何东西。

我也想过使用字典,把问题分配给键,把答案分配给值,但我不知道如何随机化,也不知道这是否是个好主意。

如果你能帮助我,我将不胜感激。

只需用这个拆分的值创建一个字典问题是关键,答案将包含字典列表

{"问题":{"a":"b":"c":"d":"a","one_answers":"a&"}]}

我建议使用.json文件,其中包含以下内容:

{
"question1": ["a", "b", "c", "d", {"correct answer": "c"}],
"question2": ["a", "b", "c", "d", {"correct answer": "d"}]
}

甚至更好:

{
"question1": ["a", "b", "c", "d", "c"],
"question2": ["a", "b", "c", "d", "d"] # The correct answer is the last letter in the list
}

或者,如果您也希望显示选项:

{
"question1": {"a": "Macedonia", "b": "Kosovo", "c": "Australia", "d": "Austria", "correct": "c"}
}

要做到这一点,您需要导入名为json的内置模块。

您可以通过以下方式访问:

with open(r"file.json", 'r') as file:
questions = json.load(file)
for question in questions.keys():
answer = input(question)
if (answer.lower() == questions[question][-1]):
# The answer is correct

或者,如果您选择我上面为.json文件建议的最后一个选项:

for question in questions.keys():
answer = input(question)
if (answer.lower() == questions[question]["correct"]):
# The answer is correct

由于random模块提供了random.choice功能,您应该更好地在questions.keys():迭代的return上使用它

import random
while True:
question = random.choice(questions)
# all the other stuff...

相关内容