我以三种不同的方式尝试了该程序,我知道我已经接近了几次,但是失败了很多次之后,我放弃了,需要额外的眼睛。 我知道该程序是“简单的”,但我知道我已经过度思考了。

程序应在列表中存储正确答案。 使用该列表对20个问题进行评分。 然后阅读该student.txt文件,以确定学生如何回答。 读取.txt文件后,应进行评分,然后显示通过或不通过(通过= 15或更高),最后显示总数或正确,不正确的答案以及学生遗漏的问题列表。

以下是所有三种尝试。 任何帮助是极大的赞赏。


answerKey = [ B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A,]

studentExam = [ B, D, A, D, C, A, B, A, C, A, B, C, D, A, D, C, C, B, D, A,]

index = 0 

numCorrect = 0

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 ')

answerKey.close()
studentExam.close()

# This program stores the correct answer for a test
# then reads students answers from .txt file
# after reading determines and dislpays pass or fail (15 correct to pass)
# Displays number of correct and incorrect answers for student
# then displays the number for the missed question/s

#Creat the answer list
def main ( ): 
    # Create the answer key list
    key = [ B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A,]

    print (key)

# Read the contents of the student_answers.txt and insert them into a list
def read_student( ):
    # Open file for reading
    infile = open ( 'student_answers.txt', 'r' )

    # Read the contents of the file into a list
    student = infile.readlines ( )

    # Close file
    infile.close ( )

    # Strip the \n from each element
    index = 0
    while index < len(student):
        student[index] = student[index].rstrip ( '\n' )

    # Print the contents of the list
    print (student)

# Determine pass or fail and display all results 
def pass_fail(answers, student):

    # Lists to be used to compile amount correct, 
    # amount wrong, and questions number missed
    correct_list = []
    wrong_list = []
    missed_list = []

    # For statement to compile lists
    for ai,bi in zip (key,student):
        if ai == bi:
            correct_list.append(ai)
        else:
            wrong_list.append(ai)
            missed_list.append(ai)

    # Printing results for user
    print(correct_list,' were answered correctly')

    print(wrong_list,' questions were missed')

    # If statement to determine pass or fail
    if len(correct_list) >=15:
        print('Congrats you have passed')
    else: 
        print('Sorry you have faild please study and try, \
         again in 90 days')
        print('Any attempt to retake test before 90 days, \
        will result in suspension of any licenses')

    # Displaying the question number for the incorrect answer
    print ( 'You missed questions number ', missed_list)


main()

a = (1, 'A'),(2,'C'),(3,'B')
b = (1,'A'), (2,'A'),(3,'C')

correct_list = []

wrong_list = []

missed_list = []

for ai, bi in zip (a, b):
    if ai == bi:
        correct_list.append(ai)
    else:
        wrong_list.append(ai)
        missed_list.append(ai)
index(ai)+1


print(correct_list,'answered correct')

print(wrong_list, 'answered wrong')

if len(correct_list) >=2:
    print ('Congrats you have passed')
else:
    print ('Sorry you have faild please study and try again in 90 days')
    print('Any attempt to retake test before 90 days will result in suspension of any     lisences')


print ('Question number missed', missed_list)

===============>>#1 票数:2

因此,我决定从您的第二个示例开始,因为这是我最容易遵循的示例。

这是一个经过修改的文件,带有说明,可以满足我的要求。 我假设学生的答案在一个名为student_answers.txt的文本文件中,并且每行有一个答案。

#!/usr/bin/env python

def read_student(path):
    with open(path, 'r') as infile:
        contents = infile.read()

    # Automatically gets rid of the newlines.
    return contents.split('\n') 

def pass_fail(answers, student):
    correct_list = []
    wrong_list = []

    # Use the 'enumerate' function to return the index.
    for index, (ai, bi) in enumerate(zip(answers, student)):
        # since problems start with 1, not 0
        problem_number = index + 1 

        if ai == bi:
            correct_list.append(problem_number)
        else:
            wrong_list.append(problem_number)

    # Print the length of the list, not the list itself.
    print(len(correct_list), 'were answered correctly')
    print(len(wrong_list), 'questions were missed')

    if len(correct_list) >= 15:
        print('Congrats, you have passed')
    else: 
        print('Sorry, you have failed. Please study and try again in 90 days')
        print('Any attempt to retake test before 90 days will result in suspension of any licenses')

    # Display each missed number on a separate line
    print ('You missed questions numbers:')
    for num in wrong_list:
        print('    ' + str(num))



def main( ): 
    key = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 
        'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']
    student_answers = read_student('student_answers.txt')
    pass_fail(key, student_answers)

if __name__ == '__main__':
    main()

一些一般性的评论:

  • main函数中,您创建了一个密钥,将其打印出来,并尝试在pass_fail函数中对其进行访问。 那是行不通的-当您在函数内部创建变量时,无法在函数外部自动访问该变量。 您可以完成几种解决方案:
  • 您读取学生档案的代码太复杂了。 您可以使用with语句,它将自动为您打开和关闭文件对象( infile )。 同样,Python带有内置方法来帮助拆分文本文件中的字符串并删除换行符。 另外,为了将来,您也可以像下面这样遍历文件中的每一行,而不用手动循环遍历它:

     with open('file.txt') as my_file: for line in my_file: print(line) # The newline is still a part of the string! 
  • 我不知道为什么你有两个wrong_listmissed_list内部变量pass_fail 我删除了missed_list

  • for循环的问题是您存储的是正确答案,而不是问题编号。 为了解决这个问题,我使用了内置的enumerate函数,它对于这种事情非常有用。 或者,您可以使用变量,并在每个循环中将其递增一次。
  • 您的print声明有些不正确,所以我整理了一下
  • 为用户打印结果时,您不想打印列表。 而是要打印列表的长度
  • 我不知道这是否是有意的,但是您的密钥中没有字符串,而只有字母名称。

===============>>#2 票数:0

您的第一个答案非常接近,除了Python将A,B,C和D解释为变量而不是字符。 您将评分逻辑留在了for循环中。 将数组更改为字符而不是变量后,得到以下结果:

 The number of correctly answered questions:  1
 The number of incorrectly answered questions:  19
 Your grade is 5 %
 You have not passed 
 The number of correctly answered questions:  2
 The number of incorrectly answered questions:  18
 Your grade is 10 %
 You have not passed 
 The number of correctly answered questions:  3
 The number of incorrectly answered questions:  17
 Your grade is 15 %
 You have not passed 
You got that question number 4 wrong
 the correct,        answer was ['A'] but you answered ['D']
 The number of correctly answered questions:  3
 The number of incorrectly answered questions:  17
 Your grade is 15 %
 You have not passed 
 The number of correctly answered questions:  4
 The number of incorrectly answered questions:  16
 Your grade is 20 %
 You have not passed 
 The number of correctly answered questions:  5
 The number of incorrectly answered questions:  15
 Your grade is 25 %
 You have not passed 
 The number of correctly answered questions:  6
 The number of incorrectly answered questions:  14
 Your grade is 30 %
 You have not passed 
 The number of correctly answered questions:  7
 The number of incorrectly answered questions:  13
 Your grade is 35 %
 You have not passed 
 The number of correctly answered questions:  8
 The number of incorrectly answered questions:  12
 Your grade is 40 %
 You have not passed 
You got that question number 10 wrong
 the correct,        answer was ['D'] but you answered ['A']
 The number of correctly answered questions:  8
 The number of incorrectly answered questions:  12
 Your grade is 40 %
 You have not passed 
 The number of correctly answered questions:  9
 The number of incorrectly answered questions:  11
 Your grade is 45 %
 You have not passed 
 The number of correctly answered questions:  10
 The number of incorrectly answered questions:  10
 Your grade is 50 %
 You have not passed 
 The number of correctly answered questions:  11
 The number of incorrectly answered questions:  9
 Your grade is 55 %
 You have not passed 
 The number of correctly answered questions:  12
 The number of incorrectly answered questions:  8
 Your grade is 60 %
 You have not passed 
 The number of correctly answered questions:  13
 The number of incorrectly answered questions:  7
 Your grade is 65 %
 You have not passed 
 The number of correctly answered questions:  14
 The number of incorrectly answered questions:  6
 Your grade is 70 %
 You have not passed 
 The number of correctly answered questions:  15
 The number of incorrectly answered questions:  5
 Your grade is 75 %
 You have not passed 
 The number of correctly answered questions:  16
 The number of incorrectly answered questions:  4
 Your grade is 80 %
 Congrats you have passed 
 The number of correctly answered questions:  17
 The number of incorrectly answered questions:  3
 Your grade is 85 %
 Congrats you have passed 
 The number of correctly answered questions:  18
 The number of incorrectly answered questions:  2
 Your grade is 90 %
 Congrats you have passed 
Traceback (most recent call last):
  File "a1.py", line 39, in <module>
    answerKey.close()
AttributeError: 'list' object has no attribute 'close'

最后一个错误是因为您曾经关闭了一个数组,该数组可能是一个文件对象,但是非常接近。 只是一个小的语法错误。

===============>>#3 票数:0

简短的版本,但能胜任。

correct = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A']
file_handler = open('student_answers.txt','r')
answers = file_handler.readlines()
valid = 0
for element in enumerate(correct):
    if answers[element[0]].rstrip("\n") == element[1]:
        valid += 1
if valid >= 15:
    print("Congrats, you passed with " + str((valid/20)*100) + "%")
else:
    print("Sorry, you failed with " + str((valid/20)*100) + "%")

  ask by PythonRetard translate from so

未解决问题?本站智能推荐:

2回复

从2D python列表中拉出某些元素?

作为测试提取大型图块的某些元素(在程序中输入为2D列表)的正确方法的简便方法,我将以下内容放入Python Shell: 在尝试测试时,我得到了: 从输入test[:][2]我希望test1返回[3,6,9] 有人可以向我解释为什么我得到了[7,8,9]吗? 另外,如
3回复

如何访问函数python中的字符串?

尝试访问此字符串以测试它是否具有3个或更多的蓝色“ b”。 --- test和three_or_more_blues都是函数。-----我完全迷失了,有人知道吗? 如果不符合我的问题,请更改标题。 真的不确定如何问这个问题。 谢谢!
2回复

python比较两个列表,将每个故障视为独立故障而不是单个故障

在单元测试中,我正在比较两个列表 我该怎么做? 我不会收到关于list_to_check_against ont sublist中每个元素失败的通知,并且因为列表很大并且我无法在stdout中看到任何内容,所以仅列出sublist列表才抑制列表的输出。 样品输出
3回复

如何从列表中删除项目?

这是我到目前为止的代码: 在remove()中编写代码以删除插槽pos中的项目,将其移出该项目以缩小间隙,并在最后一个插槽中保留值None。 关于我应该从哪里开始的任何想法?
1回复

键入列表的测试元素

有人可以就以下结果提供一些评论。 我特别感到困惑的是,当我写作时我实际上在做什么:alist = [None] * 5在#1中为什么'is'语句是False,但isinstance在#3中是真的很赞赏。
1回复

对2D数组中的元素进行顺序测试?

道歉,如果这是一个简单的问题-我是Python和numpy的新手-但非常感谢您的帮助。 我有一个二维的numpy数据数组,这些数据按行和列排列,第一列代表时间,随后的列显示每个时间点的不同参数的值。 我想从上到下读取给定的数据列(即时间= 0到时间=行数),并依次测试该列中的每个元素
1回复

Python 3.3将列表中的字符串写入txt文件

我正在尝试通过新行将列表中的字符串写入文本文件 该字符串存储在列表中,如下所示 name:emailaddress:mobilenumber 我在一个类中有一个repr ,它在屏幕上打印出一个可读的字符串,因此,如果用户要将联系人打印到屏幕上,他们将按照以下方式进行打印:
1回复

如何在R中创建两个列表的最小列表?

我试图基于线程在R中创建两个列表的最小列表。 如何制作出色的R可重现示例? 但作为基本示例,它没有涵盖列表列表。 以下结构的所需最小数据样本,其中B长度可以最小化,但必须是异构的 进行dput(files) ,您会得到太大的示例数据。 进行reproduce(files) ,情况相
3回复

如何在JUnit测试中测试列表?

我创建了Survey.java类和SurveyTest.java JUnit测试。 但是我不确定如何在Survey类中测试列表。 如何在JUnit测试中测试它们? Survey.java SurveyTest.java
3回复

模拟列表并尝试对其进行迭代

当前正在使用Mockito从我的一个类中测试一种方法。 我的班级包含一个List,并且该方法接受同一班级的对象。 问题是当我尝试从对象遍历List时。 我得到一个指向列表的空指针。 在下面,您将看到代码片段。 上面可以看到我的模拟测试,下面是我的Class和方法: 公共类货