155k views
3 votes
Assume you have a student's answers for 20 questions stored in one text file and correct answers stored in another text file. Write a function named compareAnswers that can read answers from both files and return the number of correct answers in the student’s file python

1 Answer

7 votes

Answer:

see explaination for program code

Step-by-step explanation:

Code below:

#!/usr/local/bin/python3

def main():

correct_ans = compareAnswers()

print('Correct answers are: %d' % correct_ans)

def compareAnswers():

# reads the content of file correct_answer.dat and student_answer.dat

correct_ans = open('correct_answer.dat')

stud_ans = open('student_answer.dat')

# adding correct answers to the list

c_ans = list()

for each_ans in correct_ans:

c_ans.append(each_ans)

# verifying how many answers are correct

i = 0

cnt_correct_ans = 0

for each_ans in stud_ans:

if each_ans == c_ans[i]:

cnt_correct_ans += 1

i += 1

return cnt_correct_ans

if __name__=='__main__':

main()

Go to attachment for screenshot of source code and output

Assume you have a student's answers for 20 questions stored in one text file and correct-example-1
Assume you have a student's answers for 20 questions stored in one text file and correct-example-2
User Phillip Wood
by
5.4k points