2

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

2
  • 1
    while index in answers == student_answers: this is not doing what you think it is doing Commented Apr 24, 2017 at 0:29
  • Was there a question? Commented Apr 24, 2017 at 0:29

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

I also removed your file aspect just to create content to use. You can replace the student_answers back to the way it was.
I tried your method, it seemed to help, only issue I have now is when I do the readlines for the .txt file i created which was just by saving notepad with answers posted vertically vs horizontally, and it was done just by copying the answers the professor provided as a test and pasting into notepad. When trying to do with it pasted in there horizontally it says the range has met it's max...i'm guessing it's taking these answers as a 1 element vs 20? But I also changed my format a bit by using infile = open('student_solution.txt', 'r') and student_answers = infile.readlines().
You have two ways to go about this then. you can turn each file into a list or you can turn each line into a list. It really depends on the whole setup. The code above was just to give you an idea of how you can handle a list comparison. You can create the lists seperately and then compare them like above or you can do it as an inline comparison which means reading each element from it's source. It really depends on the structure of the data you are getting.
-1
for index, value in enumerate(answers):
    total += int(value == student_answers[index])

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

1 Comment

Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.