Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Why am I getting a list index out of range error how can I fix it? def storedAnswerKey_File( fileName, correctAnswers_list): AnswerKey_File = open( fileName, w)
Why am I getting a list index out of range error how can I fix it?
def storedAnswerKey_File( fileName, correctAnswers_list): AnswerKey_File = open( fileName, "w") totalQuestions = len( correctAnswers_list) for userAnswer_Index in range( totalQuestions ): userAnswer = input( "Enter your answer for question " +\ str( (userAnswer_Index + 1 ) ) + ": " ) AnswerKey_File.write(userAnswer + " " ) AnswerKey_File.close() def FTLStoredAnswerKey_File ( fileName ): AnswerKey_File = [] storedAnswerKey_File = open( fileName, "r" ) for finalAnswer in storedAnswerKey_File: AnswerKey_File.append( finalAnswer ) return AnswerKey_File def totalCorrectAnswers( finalCorrectAnswer_list,\ enteredAnswer_list ): correctAnswers = 0 totalQuestions = len( finalCorrectAnswer_list) for current_QuestionIndex in range( totalQuestions ): if enteredAnswer_list[ current_QuestionIndex ] ==\ finalCorrectAnswer_list[ current_QuestionIndex ]: correctAnswers = correctAnswers + 1 return correctAnswers def totalIncorrectAnswers( enteredCorrectAnswers, enteredQuestions ): enteredIncorrectAnswers = enteredQuestions - enteredCorrectAnswers return enteredIncorrectAnswers def collectIncorrectAnswers( finalCorrectAnswer_list,\ enteredAnswer_list ): incorrectQuestions_list = [] totalQuestions = len( finalCorrectAnswer_list ) for current_QuestionIndex in range( totalQuestions ): if enteredAnswer_list[current_QuestionIndex] != \ finalCorrectAnswer_list[ current_QuestionIndex ]: incorrectQuestions_list.append( current_QuestionIndex + 1 ) return incorrectQuestions_list def examPass( finalPassmark, enteredAnswer_list ): if len (enteredAnswer_list ) >= finalPassmark: return True else: return False def printValues_list( anyList ): for currentValueIndex in range( len( anyList) ): print( anyList[ currentValueIndex ] ) def printResults( entireExamAnswers_correct, entireExamAnswers_incorrect, entireExamAnswers_incorrectlist ): print( "Total Correct Answers: " +\ str( entireExamAnswers_correct),\ "Total Incorrect Answers: " +\ str( entireExamAnswers_incorrect ) ) printValues_list( entireExamAnswers_incorrectlist ) def main(): correctAnswers_list = ["B", "D", "A", "A", "B", "A", "B", "A", "C", "D",\ "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"] Number_of_Questions = len(correctAnswers_list) FILE_NAME = " studentAnswers.txt " Pass_Mark = 15 storedAnswerKey_File( FILE_NAME, correctAnswers_list ) studentAnswersList = FTLStoredAnswerKey_File( FILE_NAME ) num_correctAnswers = totalCorrectAnswers(correctAnswers_list,\ studentAnswersList ) num_incorrectAnswers = totalIncorrectAnswers( num_correctAnswers,\ Number_of_Questions ) incorrectQuestions_list = collectIncorrectAnswers( correctAnswers_list, studentAnswersList) printResults( num_correctAnswers, num_incorrectAnswers, incorrectQuestions_list ) if examPass( Pass_Mark,studentAnswersList): print(" The Student passed the exam") else: print(" The Student did not pass the exam") main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started