Devin Lathsaw
Devin Lathsaw

Reputation: 21

Using Python to compare lists?

I'm fairly new to python, just started the course this semester. I'm struggling with trying to figure out a way to write a code that takes the correct answers and stores them as list, then reads the student answers for each of the 20 questions from a txt file and stores the answers in another list. After that I want to compare the lists and then prints their answers and the program will display a message indicating if the student passed or not, (15 or greater correct is a pass) and total number correct and total number incorrect. so for example the correct answers as A, C, A, A, D, B, C, A, C, B, A, D, C, A, D, C, B, B, D, A. for the student answers would just be a create your own text file to test. Any help would be appreciated my current format doesn't seem to work, which is shown below.

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

HERE IS THE UPDATED PROGRAM that still seems to give issues. The student answers I'm using are 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()

Upvotes: 2

Views: 550

Answers (3)

Luchko
Luchko

Reputation: 1153

You can calculate total zipping both lists in this way

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

it is more than 2 times faster than incrementing total in this more compact but slower way:

total += int(stdnt_ans == correct_ans)

Upvotes: 2

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22875

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

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

Upvotes: -1

Aklys
Aklys

Reputation: 481

This will get your total. Just needed to adjust the way you were doing your loop.

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

Upvotes: 0

Related Questions