社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python测试程序-读取csv并解析问题/答案

spiritoffire224 • 5 年前 • 405 次点击  

我有一个简单的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,"%.")
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40245
 
405 次点击  
文章 [ 2 ]  |  最新文章 5 年前
arsho
Reply   •   1 楼
arsho    6 年前
  • 重新排列列表并首先选择 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

改进:

参考文献:

Adam Smith
Reply   •   2 楼
Adam Smith    6 年前

这里有一个stdlib库: 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 .

quiz = itertools.sample(quiz_qas, num_questions)
for q, a in quiz:
    # etc