2017-04-24 102 views
2

我对python相当陌生,刚开始这个学期的课程。我努力想办法编写一个代码,将正确的答案存储为列表,然后从txt文件中读取20个问题中的每一个的学生答案,并将答案存储在另一个列表中。之后,我想比较列表,然后打印答案,程序将显示一条消息,指示学生是否通过(15或更大的正确是通过),总数正确,总数不正确。所以例如正确答案为A,C,A,A,D,B,C,A,C,B,A,D,C,A,D,C,B,B,D,A。学生的答案只是创建自己的文本文件来测试。任何帮助,将不胜感激我目前的格式似乎并不奏效,如下所示。使用Python比较列表?

高清的main():

total = 0 
index = 0 
answers = [ 'A', 'C', 'A', 'A', 'D',\ 
      'B', 'C', 'A', 'C', 'B',\ 
      'A', 'D', 'C', 'A', 'D',\ 
      'C', 'B', 'B', 'D', 'A'] 

student_answers = open('student_solution.txt', 'r') 

for answer in student_answers: 
    print(answer.strip()) 

    while index in answers == student_answers: 
     if student_answers[0] == answers[0]: 
      total +=1 
     else: 
      total +=0 



student_answers.close() 
print('Total correct answers: ', total) 
print('Total of incorrect answers: ', 20 - total) 

if total >= 15: 
    print('Congratulations! You passed the exam.') 
else: 
    print('Sorry, you have failed the exam.') 

的main()

这里是更新的程序仍然似乎给问题。我使用的是学生的答案是 ACAADBCACBADCADCBBDAC AADBCACBADCADCBBDD

高清的main():

total = 0 
index = 0 
answers = [ 'A', 'C', 'A', 'A', 'D',\ 
      'B', 'C', 'A', 'C', 'B',\ 
      'A', 'D', 'C', 'A', 'D',\ 
      'C', 'B', 'B', 'D', 'A'] 

infile = open('student_solution.txt', 'r') 

student_answers = infile.readline() 
infile.close() 
print(student_answers) 

for answer in student_answers: 
    for y in range(len(answer)): 
     if answer[y] == answers[y]: 
      total += 1 


print('Total correct answers: ', total) 
print('Total of incorrect answers: ', 20 - total) 

if total >= 15: 
     print('Congratulations! You passed the exam.') 
else: 
     print('Sorry, you have failed the exam.') 

的main()

+1

'而指数的答案== student_answers:'这不是做你认为它是做什么 –

+0

有没有问题? –

回答

2

可以计算total这样

total = 0 
for stdnt_ans,correct_ans in zip(student_answers, answers): 
    if stdnt_ans == correct_ans: 
     total += 1 
荏苒两个列表

它比增加速度快2倍以上total在这个更紧凑但更慢的方式:

total += int(stdnt_ans == correct_ans) 
0

这将得到您的总和。只需要调整你做循环的方式。

def main(): 

    total = 0 
    index = 0 
    answers = [ 'A', 'C', 'A', 'A', 'D',\ 
       'B', 'C', 'A', 'C', 'B',\ 
       'A', 'D', 'C', 'A', 'D',\ 
       'C', 'B', 'B', 'D', 'A'] 

    student_answers = [[ 'A', 'C', 'A', 'A', 'D',\ 
       'B', 'C', 'D', 'C', 'B',\ 
       'A', 'D', 'C', 'A', 'D',\ 
       'C', 'B', 'A', 'D', 'A'],\ 
       [ 'A', 'C', 'D', 'D', 'D',\ 
       'A', 'C', 'A', 'D', 'B',\ 
       'D', 'D', 'C', 'A', 'D',\ 
       'B', 'B', 'B', 'D', 'A']] 

    for answer in student_answers: 
     for i in range(len(answer)): 
      if answer[i] == answers[i]: 
       total += 1 
     print('Total correct answers: ', total) 
     print('Total of incorrect answers: ', 20 - total) 
     if total >= 15: 
      print('Congratulations! You passed the exam.') 
     else: 
      print('Sorry, you have failed the exam.') 
     total = 0 

main() 
+0

我还删除了文件方面,以创建要使用的内容。您可以将student_answers替换为原来的样子。 – Aklys

+0

我试过你的方法,它似乎有所帮助,现在只有问题是当我为我创建的.txt文件创建readlines时,它只是通过垂直与水平方向发布的答案保存记事本,并且仅通过复制回答教授提供的测试并粘贴到记事本中。当试图处理它粘贴在那里水平说它的范围已经达到它的最大值...我猜这是把这些答案作为一个元素与20?但是我也通过使用infile = open('student_solution.txt','r')和student_answers = infile.readlines()来改变我的格式。 –

+0

然后你有两种方法可以解决这个问题。您可以将每个文件转换为列表,也可以将每行转换为列表。这真的取决于整个设置。上面的代码只是为了让你了解如何处理列表比较。您可以单独创建列表,然后像上面那样比较它们,或者可以将它作为内联比较来进行,即从源代码读取每个元素。这真的取决于你所得到的数据结构。 – Aklys

-1
for index, value in enumerate(answers): 
    total += int(value == student_answers[index]) 

print('Pass' if total >= 15 else 'Fail') 
+0

虽然这段代码片段是受欢迎的,并且可能会提供一些帮助,但它会[如果它包含解释](/ meta.stackexchange.com/q/114762)*如何解决该问题将会[大大改进。没有这些,你的答案就没有什么教育价值了 - 记住,你正在为将来的读者回答这个问题,而不仅仅是现在问的人!请编辑您的答案以添加解释,并指出适用的限制和假设。 –