0

I'm tasked with creating a program that simulates a scantron being turned on from an off state, once turned on it compares two arrays and displays a student's grade based on the comparison of the two arrays. I'm also tasked with creating classes.

I can get the machine to turn on but the grading part is where I'm having trouble. My program tells me that 'correct is not defined' in 'grade = Answers (correct, student, theGrade)'. I don't think I returned or didn't pass the variable correctly. Below is the code. Can anyone PLEASE help??

#this program creates a scantron grading system
#the machine will be turned on, scan the correct answers,
#then will grade the student's answers, and display results

#start class initializing machine off and then switching on
class quizGrader:

 #initialize state of machine off
 def __init__(self):

     self.power = 'Off'

 #create module to ask if the user would like to turn on the machine
 def switch(self):

     #ask user if they'd like to turn on the machine
     powerOn = input('Would you like to turn the machine on? (enter Y for yes)')

     #create if statement for system to be turned on or off
     if powerOn == 'y' or powerOn == 'Y':
         self.power = 'On'

     else:
         self.power = 'Off'

 def get_power(self):
     return self.power

#create class for correct answers, student answers, and
#for the student's scantron to be graded
class Answers:

 #initialize the grades
 def __init__(self, correct, student, theGrade):

     #declare what each self. will be equal to
     self.__correctAnswers = correct
     self.__studentAnswers = student
     self.__studentGrade = theGrade

 #create set method for correctAnswers
 def set_correctAnswers(self, correct):
     self.__correctAnswers = correct
     correct = ['A','B','C','D','E','E','D','C','B','A', \
               'A','B','C','D','E','E','D','C','B','A']
     print('Correct answers have been recorded...')

     return correct
 #create set method for studentAnswers
 def set_studentAnswers(self, student):
     self.__studentAnswers = student
     student = ['B','B','C','D','A','E','D','C','B','A', \
               'A','B','C','D','E','E','D','C','B','A']

     return student
 #create set method for student's scantron to be graded
 def set_studentGrade(self, theGrade):
     self.__studentGrade = theGrade

     right = 0
     index = 1

     #create a for loop and if statement for the right answers
     #to be counted and then calculate the % of the grade
     for index in range(0,20):
        if self.__correctAnswers [index] == self.__studentAnswers [index]:
            right += 1
            percent = float(right/20)
            percent = theGrade

     return theGrade

 #return all the methods previously created
 def get_correctAnswers(self, correct):
    return self.__correctAnswers

 def get_studentAnswers(self, student):
    return self.__studentAnswers

 def get_studentGrade(self, theGrade):
    return self.__studentGrade

#start main module
def main():

 #create an object from the quizGrader class
 machine = quizGrader()

 #display that the machine is off
 print('The machine is powered:', machine.get_power())

 #ask the user if they'd like to turn the machine on
 machine.switch()

 #display the user's choice to turn on or leave machine powered off
 print('The machine is now/still powered:',machine.get_power())

 #create an object from the Answers class
 grade = Answers(correct, student, theGrade)


 #display that the correct answers have been recorded and display the answers
 print('Correct answers have been recorded:',grade.get_correctAnswers())
 print()
 print()
 #display that the student's answers have been recorded and display the answers
 print('Student answers have been recorded:', grade.get_studentAnswers())
 print()

 #grade the student's answers
 grade.studentGrade()

 #display the amount of answers student answered correctly
 #and the percentage of their grade based on correct answers
 print('The student grade is',right,'out of 20 or',percent,'%.')

#close main function    
main()
Rozay
  • 13
  • 7

2 Answers2

0

The variable correct is not defined in your main function.

For example, you could simply add the line correct = 'C' before the line grade = Answers(correct, student, theGrade) to show that the correct answer is C.

The finished product would look something like this:

...
correct = 'C'
grade=Answers(correct, student, theGrade)
...
colelemonz
  • 1,229
  • 1
  • 15
  • 27
0

You have not defined what correct or student or even theGrade are

grade = Answers(correct, student, theGrade)

You need to instantiate the classes you've created, but you need to actually have those variables available in the main() function.

For example:

def main():

 #create an object from the quizGrader class
 machine = quizGrader()

 correct = ['A','B','C','D','E','E','D','C','B','A', \
               'A','B','C','D','E','E','D','C','B','A']

 student = ['B','B','C','D','A','E','D','C','B','A', \
               'A','B','C','D','E','E','D','C','B','A']

will get you a little farther... Just because you declare them in the classes above doesn't mean they're available.

crackpotHouseplant
  • 380
  • 1
  • 3
  • 12
  • Thanks! but I put the array in the class because I have to create: • A constructor (def __init__(self) in Python) that creates a quizGrader with an initial state of off. • A set method that will turn the machine on. • A set method that initializes an array of correct answers. • A set method that initializes an array of student answers. • A work method that grades student answers. • A get method that obtains the number of correct answers. • A work method that displays the number of correct answers. – Rozay Apr 17 '16 at 21:03
  • You should set class members instead of trying to pass them in the constructor – crackpotHouseplant Apr 17 '16 at 21:07
  • thanks for the help. I fixed it but still having issues with calling the lists to be graded. – Rozay Apr 18 '16 at 19:54
  • 'code' def gradeTest(self, correct, student): right = 0 index = 1 for index in range(0,20): if correct[index] == student[index]: right += 1 percent = float(right/20) self.gradeTest =('The student\'s grade is',right,'out of 20 or',format(percent,'.2f')'%.') def get_correctAnswers(self): return self.correctAnswers def get_studentAnswers(self): return self.studentAnswers def get_gradeTest(self): return self.gradeTest – Rozay Apr 18 '16 at 20:06
  • I'm also a newb and don't know how to put code in the comment box. Sorry. – Rozay Apr 18 '16 at 20:07
  • You should just update your original question if your changes do not change the substance of the question. If the changes do, you should make a new question. Also, if I've answered your question, I'd appreciate you accept my answer. – crackpotHouseplant Apr 18 '16 at 21:23