创建Scantron以在Python

时间:2016-04-16 19:27:31

标签: python arrays oop object initializer

我的任务是创建一个模拟扫描仪从关闭状态打开的程序,一旦打开它就会比较两个数组,并根据两个数组的比较显示学生的等级。我还负责创建课程。

我可以打开机器但是评分部分是我遇到麻烦的地方。我的程序告诉我'正确未定义'在'年级=答案(正确,学生,同志)'。我不认为我回来了或没有正确传递变量。下面是代码。任何人都可以帮忙吗?

#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()

2 个答案:

答案 0 :(得分:0)

NSArray *viewControllers = self.navigationController.viewControllers; UIViewController *rootViewController = (UIViewController *)[viewControllers objectAtIndex:viewControllers.count - 2]; //index of your root viewcontroller 函数中未定义变量correct

例如,您只需在第main行之前添加第correct = 'C'行即可显示正确答案为grade = Answers(correct, student, theGrade)

成品看起来像这样:

C

答案 1 :(得分:0)

您尚未定义correctstudent甚至theGrade

的内容
grade = Answers(correct, student, theGrade)

您需要实例化您创建的类,但您需要在main()函数中实际提供这些变量。

例如:

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']

会让你更远一点...... 仅仅因为你在上面的类中声明它们并不意味着它们可用。