0

Why does it keeps giving me this error? I have this in my first method but it doesn't give me an error, but when I use the code in the 2nd method it continue giving me this error

This is my first method, it works well,

    def btn_English(self):
    answerKey = open("answerkeyEnglish", 'r')
    studentExam = open("studentExam", 'r')
    index = 0
    numCorrect = 0

    for answerLine, studentLine in zip(answerKey, studentExam):
        answer = answerLine.split()
        studentAnswer = studentLine.split()
        if studentAnswer != answer:
            print("You got question number", index + 1, "wrong\nThe correct answer was", answer, "but you answered",
                  studentAnswer)
            index += 1
        else:
            numCorrect += 1
            index += 1
    gradeAverage = int((numCorrect / 2) * 100)
    grade = numCorrect
    english = open('examResult','w')
    english.write(str(grade))

and this the second method, where it gives the error. in english.write(str(grade)[2]).

    def btn_Math(self):
    answerKey = open("answerkeyEnglish", 'r')
    studentExam = open("studentExam", 'r')
    index = 0
    numCorrect = 0

    for answerLine, studentLine in zip(answerKey, studentExam):
        answer = answerLine.split()
        studentAnswer = studentLine.split()
        if studentAnswer != answer:
            print("You got question number", index + 1, "wrong\nThe correct answer was", answer, "but you answered",
                  studentAnswer)
            index += 1
        else:
            numCorrect += 1
            index += 1
    gradeAverage = int((numCorrect / 2) * 100)
    grade = numCorrect
    english = open('examResult','r')
    english.write(str(grade)[2])

Can someone tell me why I'm getting this error because in the first method it works well, but in the 2nd method it gives error?

1 Answers1

0

str(grade)[2] gives the third character of str(grade), which only exists if grade is a number with 3 or more digits.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89