首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用外部文本文件创建选择题程序?

如何使用外部文本文件创建选择题程序?
EN

Stack Overflow用户
提问于 2022-01-25 04:54:07
回答 2查看 555关注 0票数 0

我试图创建一个选择题程序,它将读取文本文件,按随机顺序提出5个问题,接收答案,如果答案为假,则显示答案。下面是我写的文章:

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

mcq.txt:

代码语言:javascript
复制
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

该方案:

代码语言:javascript
复制
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‘对象不支持项目分配。

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

代码语言:javascript
复制
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()放在这里,但它似乎并没有改变任何事情。

我还考虑过使用字典,并将问题分配给键和值的答案,但我不知道如何将其随机化,以及这是否是一个好主意。

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

EN

回答 2

Stack Overflow用户

发布于 2022-01-25 05:01:11

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

{“问题”:{“a”:“”、"b":"“、"c":"”、"d":"“、”ans“:”a“}

票数 0
EN

Stack Overflow用户

发布于 2022-01-25 05:19:18

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

代码语言:javascript
复制
{
    "question1": ["a", "b", "c", "d", {"correct answer": "c"}],
    "question2": ["a", "b", "c", "d", {"correct answer": "d"}]
}

甚至更好:

代码语言:javascript
复制
{
    "question1": ["a", "b", "c", "d", "c"],
    "question2": ["a", "b", "c", "d", "d"] # The correct answer is the last letter in the list
}

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

代码语言:javascript
复制
{
    "question1": {"a": "Macedonia", "b": "Kosovo", "c": "Australia", "d": "Austria", "correct": "c"}
}

为此,需要导入名为

的内置模块json

您可以这样访问:

代码语言:javascript
复制
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文件建议的最后一个选项:

代码语言:javascript
复制
for question in questions.keys():
    answer = input(question)
    if (answer.lower() == questions[question]["correct"]):
        # The answer is correct

由于random模块提供了random.choice函数,所以最好在由questions.keys()编辑的可迭代return上使用它。

代码语言:javascript
复制
import random
while True:
    question = random.choice(questions)
    # all the other stuff...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70843591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档