Python测验程序-读取CSV并解析问题/答案

时间:2018-12-01 03:39:37

标签: python

我在Python中进行了一个简单的测验,但遇到了问题。它以“问题,答案”格式读取逗号分隔的文本文件。该程序正在仔细阅读测验并输出问题,但我无法将问题随机化,也无法通过“ CorrectAnswer”变量来获取正确答案。

例如,如果问题是“加利福尼亚的首都在哪里?”答案是“萨克拉曼多”,则CSV文件的格式如下:“加利福尼亚萨克拉曼多的首都是什么”。但是该程序无法引用答案,只能引用逗号之前的第一部分。

我在做什么错了?

def quiz():
    score=0
    questionsRight=0
    fileName = input("Please enter the name of the quiz file: ")
    quizFile = open(fileName,"r")
    quizData = quizFile.readlines()
    questionno=1
    for x in range(10):
        for x in quizData:
            data = x.split(",")
        random.shuffle(quizData)
        questions = data[0]
        CorrectAnswer = data[1]

        print("Question #",questionno)
        print(questions)
        answer = input("What is your answer? ")
        if answer == CorrectAnswer:
            print("Correct!")
            score=score+1
            questionsRight=questionsRight+1
            questionno = questionno+1

        else:
            print("Incorrect.")
            questionno = questionno+1

    totalScore = (score / 10) * 100
    print("You got ",score," questions right, and a score of ",totalScore,"%.")

2 个答案:

答案 0 :(得分:2)

对此有一个stdlib库:Parameter(s) specified in the command line: -m lab51.mod -o lab51.txt --tmlim 300 Reading model section from lab51.mod... lab51.mod:58: syntax error in expression Context: ) + sum { m in MES } ( Std [ m ] - sum { i in MAYORISTA } * MathProg model processing error

csv

import csv with open("path/to/your/quizfile.csv") as f: reader = csv.reader(f) quiz_qas = list(reader) # this could get *very* large -- take care if you have a large file. q, a = random.choice(quiz_qas) print(q) answer = input(">>> ") if answer == a: # user got the right answer else: # user failed 甚至可以让您选择random.sample的随机k大小样本。

quiz_qas

答案 1 :(得分:0)

  • 随机播放列表并选择前n
  • 使用strip删除答案末尾的新行

更新的代码:

import random
def quiz():
    score=0
    questionsRight=0
    fileName = input("Please enter the name of the quiz file: ")
    quizFile = open(fileName,"r")
    quizData = quizFile.readlines()
    random.shuffle(quizData)
    questionno=1
    for i in range(5):
        x = quizData[i].strip()
        data = x.split(",")        
        question = data[0]
        CorrectAnswer = data[1]

        print("Question #",questionno)
        print(question)
        answer = input("What is your answer? ")
        if answer == CorrectAnswer:
            print("Correct!")
            score=score+1
            questionsRight=questionsRight+1
            questionno = questionno+1

        else:
            print("Incorrect.")
            print("Correc answer should be: "+CorrectAnswer)
            questionno = questionno+1
        print()

    totalScore = (score / 10) * 100
    print("You got ",score," questions right, and a score of ",totalScore,"%.")
quiz()

输出:

output

data.csv

Demo question 1,Answer 1
Demo question 2,Answer 2
Demo question 3,Answer 3
Demo question 4,Answer 4
Demo question 5,Answer 5
Demo question 6,Answer 6
Demo question 7,Answer 7
Demo question 8,Answer 8
Demo question 9,Answer 9
Demo question 10,Answer 10
Demo question 11,Answer 11
Demo question 12,Answer 12
Demo question 13,Answer 13
Demo question 14,Answer 14
Demo question 15,Answer 15
Demo question 16,Answer 16
Demo question 17,Answer 17
Demo question 18,Answer 18
Demo question 19,Answer 19
Demo question 20,Answer 20
Demo question 21,Answer 21

改进:

参考: