我已经以三种不同的方式尝试了这个程序,我知道我已经接近了好几次,但是在失败了这么多次之后,我放弃了,需要一双额外的眼睛。我知道这个程序很“简单”,但我知道我想多了。
程序应将正确答案存储在列表中。使用该列表对 20 个问题的测试进行评分。然后阅读该 student.txt 文件以确定学生的回答方式。阅读 .txt 文件后,它应该评分然后显示通过或失败(通过 = 15 或更高)它最终显示总数或正确、不正确的答案以及学生错过的问题列表。
以下是所有三种尝试。任何帮助是极大的赞赏。
answerKey = [ B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A,]
studentExam = [ B, D, A, D, C, A, B, A, C, A, B, C, D, A, D, C, C, B, D, A,]
index = 0
numCorrect = 0
for answerLine, studentLine in zip (answerKey, studentExam):
answer = answerLine.split()
studentAnswer = studentLine.split()
if studentAnswer != answer:
print( 'You got that question number', index + 1, 'wrong\n the correct,\
answer was' ,answer, 'but you answered' , studentAnswer)
index += 1
else:
numCorrect += 1
index += 1
grade = int((numCorrect / 20) * 100)
print (' The number of correctly answered questions: ', numCorrect)
print (' The number of incorrectly answered questions: ', 20 - numCorrect)
print (' Your grade is', grade, '%')
if grade <= 75:
print (' You have not passed ')
else:
print (' Congrats you have passed ')
answerKey.close()
studentExam.close()
# This program stores the correct answer for a test
# then reads students answers from .txt file
# after reading determines and dislpays pass or fail (15 correct to pass)
# Displays number of correct and incorrect answers for student
# then displays the number for the missed question/s
#Creat the answer list
def main ( ):
# Create the answer key list
key = [ B, D, A, A, C, A, B, A, C, D, B, C, D, A, D, C, C, B, D, A,]
print (key)
# Read the contents of the student_answers.txt and insert them into a list
def read_student( ):
# Open file for reading
infile = open ( 'student_answers.txt', 'r' )
# Read the contents of the file into a list
student = infile.readlines ( )
# Close file
infile.close ( )
# Strip the \n from each element
index = 0
while index < len(student):
student[index] = student[index].rstrip ( '\n' )
# Print the contents of the list
print (student)
# Determine pass or fail and display all results
def pass_fail(answers, student):
# Lists to be used to compile amount correct,
# amount wrong, and questions number missed
correct_list = []
wrong_list = []
missed_list = []
# For statement to compile lists
for ai,bi in zip (key,student):
if ai == bi:
correct_list.append(ai)
else:
wrong_list.append(ai)
missed_list.append(ai)
# Printing results for user
print(correct_list,' were answered correctly')
print(wrong_list,' questions were missed')
# If statement to determine pass or fail
if len(correct_list) >=15:
print('Congrats you have passed')
else:
print('Sorry you have faild please study and try, \
again in 90 days')
print('Any attempt to retake test before 90 days, \
will result in suspension of any licenses')
# Displaying the question number for the incorrect answer
print ( 'You missed questions number ', missed_list)
main()
a = (1, 'A'),(2,'C'),(3,'B')
b = (1,'A'), (2,'A'),(3,'C')
correct_list = []
wrong_list = []
missed_list = []
for ai, bi in zip (a, b):
if ai == bi:
correct_list.append(ai)
else:
wrong_list.append(ai)
missed_list.append(ai)
index(ai)+1
print(correct_list,'answered correct')
print(wrong_list, 'answered wrong')
if len(correct_list) >=2:
print ('Congrats you have passed')
else:
print ('Sorry you have faild please study and try again in 90 days')
print('Any attempt to retake test before 90 days will result in suspension of any lisences')
print ('Question number missed', missed_list)