使用Python比较列表?

时间:2017-04-24 00:24:38

标签: python list

我对python很新,刚开始这个学期的课程。我正在努力寻找一种方法来编写一个能够获得正确答案并将它们存储为列表的代码,然后从txt文件中读取20个问题中每个问题的学生答案并将答案存储在另一个列表中。之后,我想比较列表,然后打印他们的答案,程序将显示一条消息,指示学生是否通过,(15或更高的正确是通过)和总数正确和总数不正确。所以例如正确答案为 学生答案的A,C,A,A,D,B,C,A,C,B,A,D,C,A,D,C,B,B,D,A只是一个创造你的自己的文本文件来测试。任何帮助将不胜感激我目前的格式似乎不起作用,如下所示。

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 = 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()的

这是更新的计划,似乎仍然存在问题。学生回答我使用的是 A C A A D B C A C B A D C A D C B B D A C A A D B C A C B A D C A D C B B D D

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

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()的

3 个答案:

答案 0 :(得分:2)

您可以通过这种方式计算total压缩两个列表

total = 0
for stdnt_ans,correct_ans in zip(student_answers, answers):
    if stdnt_ans == correct_ans:
        total += 1

它比以这种更紧凑但更慢的方式递增total的速度快2倍:

total += int(stdnt_ans == correct_ans)

答案 1 :(得分: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()

答案 2 :(得分:-1)

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

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