[我正在使用txt文件进行多项选择测验,但不知道如何随机选择选项并给出分数

问题描述 投票:0回答:4
points = 0
with open("ComputerScience.txt","r") as f:
    for line in f:
        y = line.split(",")
        question = y[0]
        question1 = question
        del y[0]
        random.shuffle(y)
        answer1 = y[0]
        answer2 = y[1]
        answer3 = y[2]
        answer4 = y[3]
        print(question1+"\n","(A)",answer1,"\n","(B)",answer2,"\n","(C)",answer3,"\n","(D)",answer4)
        correctans = answer1
        userans = input("Enter A,B,C,D: ")
        while userans == correctans:
            points = (points+1)
        print(points)

“这是我的文本文件”“ >>

“这就是它的输出”

我如何随机化选项,并让用户输入A B C或D并为每个正确答案添加分数

points = 0,其中open(“ ComputerScience.txt”,“ r”)为f:对于f中的行:y = line.split(“,”)问题= y [0]问题1 =问题del y [0] random.shuffle(y)...

python text-files
4个回答
0
投票

夫妇:


0
投票
LETTERS = ['A', 'B', 'C', 'D']
score = 0
with open("ComputerScience.txt","r") as f:
    for line in f:
        question, *answers = map(str.strip, s.split(','))
        answers_jumbled = random.sample(answers, len(answers))
        print(question)
        for index, answer in enumerate(answers_jumbled):
            print(f' ({LETTERS[index]}) {answer}')
        answer = None
        while answer not in LETTERS:
            answer = input("Enter A,B,C,D: ")
        if LETTERS.index(answer) == 0:
            print('Correct')
            score += 1
        else:
            print('Wrong')
print('Score:', score)

0
投票

您需要在对数组进行混排之前使用correctans = answer1行。


0
投票
import random
© www.soinside.com 2019 - 2024. All rights reserved.