【问题标题】:Python Test grading program not quite working but close [closed]Python测试评分程序不太有效但关闭[关闭]
【发布时间】:2013-04-25 21:30:30
【问题描述】:

我正在开发一个打开文件“answer.txt”的程序。该文件模拟学生对测试的回答。该程序然后将文件与 answerKey 进行比较。它逐行打印答案键与学生的答案。它会记录好答案和坏答案。最后它打印一个分数。我可以让这个程序使用两个不同的答案键,但是当我尝试从文件中提取答案时,我得到了太多的输出。它跳过文件中的第一个答案。然后它只来回打印来自应答键的 B、D。学生方从第二个答案开始打印,然后跳过所有其他答案。

我的代码:

def main():
    try:
        answerKey = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C',\
                 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']
        index = 0
        numCorrect = 0
        answer_file = open('answers.txt', 'r')

        studentExam = answer_file.readline()

        print('Correct\tYour\tStatus\nAns.\tAns.\n-----------------------\n')
        while studentExam != "":
            problem_number = index + 1
            studentExam = studentExam.rstrip("\n")

            studentExam = answer_file.readline()

            for answerLine, studentLine in zip (answerKey, studentExam):
                answer = answerLine.split()
                studentAnswer = studentLine.split()

                if studentAnswer != answer:
                    print( 'You got that question number', index + 1, 'wrong\n the correct answer was' ,answer, 'but you answered' , studentAnswer)
                    index += 1
                else:
                    numCorrect += 1
                    index += 1

        grade = int((numCorrect / 20) * 100)

        print (' The number of correctly answered questions: ', numCorrect)

        print (' The number of incorrectly answered questions: ', 20 - numCorrect)

        print (' Your grade is', grade, '%')

        if grade <= 75:
                    print (' You have not passed ')
        else:
                    print (' Congrats you have passed ')
    except IOError:
        print("The file could not be found")
    except IndexError:
        print("There was an indexing error")
    except:
        print("An error occurred")
main()

我的输出:

You got that question number 1 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 2 wrong
the correct  answer was ['D'] but you answered []

You got that question number 3 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 4 wrong
 the correct answer was ['D'] but you answered []

You got that question number 5 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 6 wrong
 the correct answer was ['D'] but you answered []

You got that question number 7 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 8 wrong
 the correct answer was ['D'] but you answered []

You got that question number 9 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 10 wrong
 the correct answer was ['D'] but you answered []

You got that question number 12 wrong
 the correct answer was ['D'] but you answered []

You got that question number 13 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 14 wrong
 the correct answer was ['D'] but you answered []

You got that question number 15 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 16 wrong
 the correct answer was ['D'] but you answered []

You got that question number 17 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 18 wrong
 the correct answer was ['D'] but you answered []

You got that question number 19 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 20 wrong
 the correct answer was ['D'] but you answered []

You got that question number 21 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 22 wrong
 the correct answer was ['D'] but you answered []

You got that question number 23 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 24 wrong
 the correct answer was ['D'] but you answered []

You got that question number 25 wrong
 the correct answer was ['B'] but you answered ['A']

You got that question number 26 wrong
 the correct answer was ['D'] but you answered []

You got that question number 27 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 28 wrong
 the correct answer was ['D'] but you answered []

You got that question number 29 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 30 wrong
 the correct answer was ['D'] but you answered []

You got that question number 31 wrong
 the correct answer was ['B'] but you answered ['C']

You got that question number 32 wrong
 the correct answer was ['D'] but you answered []

You got that question number 34 wrong
 the correct answer was ['D'] but you answered []

You got that question number 35 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 36 wrong
 the correct answer was ['D'] but you answered []

You got that question number 37 wrong
 the correct answer was ['B'] but you answered ['D']

You got that question number 38 wrong
 the correct answer was ['D'] but you answered []

 The number of correctly answered questions:  2
 The number of incorrectly answered questions:  18
 Your grade is 10 %

比较的数据不正确,请帮我纠正这个问题。答案只有 20 个,文件中的答案如下:

B
D
A
A
C
A
B
A
C
C
C
C
D
A
D
C
C
B
D
D

每个都在它自己的行中。因此,我尝试使用循环来遍历答案键中的每个答案,并将其与该文件中的每个答案进行比较。谁能告诉我我的 python 代码有什么问题?

【问题讨论】:

    标签: python file for-loop while-loop


    【解决方案1】:

    不要遍历文件,而是一次将文件全部读入列表。然后,您还想清理数据行。这是通过以下方式完成的:

    answers = open('answers.txt', 'r').readlines()
    answers = [answer.strip() for answer in answers]
    

    这会使您的while 循环变得毫无意义,因为所有这些工作都已计算在内。

    一旦我们有了它,就可以使用该列表了。

    for answerLine, studentLine in zip(answerKey, answers):
    

    这将为您提供以下输出(如果放置正确):

    Correct    Your    Status
    Ans.    Ans.
    -----------------------
    
    ('You got that question number', 10, 'wrong\n the correct answer was', ['D'], 'but you answered', ['C'])
    ('You got that question number', 11, 'wrong\n the correct answer was', ['B'], 'but you answered', ['C'])
    ('You got that question number', 20, 'wrong\n the correct answer was', ['A'], 'but you answered', ['D'])
    (' The number of correctly answered questions: ', 17)
    (' The number of incorrectly answered questions: ', 3)
    (' Your grade is', 85, '%')
    Congrats you have passed
    

    另外,我强烈不鼓励使用空的 except 块。这将使调试您的代码成为一场噩梦,因为您将无法确定哪里出了问题以及哪里出了问题。

    【讨论】:

    • 谢谢。我实际上正在将它用于编程课程,但我领先于课程并且这本书没有很清楚地解释这个程序,但是您的输入确实有帮助。我通常不会放一个空的 except 块,但老师要求它。我通常在处理程序或编写代码时将其作为注释关闭以显示实际错误,从而使调试更容易。谢谢!
    【解决方案2】:

    您的算法不是您所期望的。您正在使用readline 循环遍历文件,然后对于该文件中的每一行,您都尝试遍历答案键。文件中的每一行都返回两个字符 ('B\n'),即成绩和换行符。当您执行for answerLine, studentLine in zip (answerKey, studentExam): 行时,您是zip使用 2 元素 studentExam 的长答案键(请记住字符串是可迭代的),因此您只会看到第一个回答键的人(第二个有空白学生答案。

    我建议在算法开始时一次性阅读学生的答案,然后在完整列表中使用zip。那会给你你想要的。

    P.S.,你可能想要这个:

    answer = answerLine.strip()
    studentAnswer = studentLine.strip()
    

    而不是这个:

    answer = answerLine.split()
    studentAnswer = studentLine.split()
    

    【讨论】:

    • 谢谢,我刚开始使用这些算法,仍然掌握了窍门。仍然是一个编程 n00b,但学习和这些信息确实有助于修复这个程序。更重要的是,解释这个问题真的很有帮助,所以我可以理解为什么循环会这样。谢谢,我会设置它一次打开文件并压缩完整列表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多