【问题标题】:Creating a Scantron to grade a test in Python创建一个 Scantron 以在 Python 中为测试评分
【发布时间】:2016-08-08 16:46:13
【问题描述】:

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

我可以让机器打开,但评分部分是我遇到问题的地方。我的程序告诉我,'grade = Answers (correct, student, theGrade)'中的'正确没有定义'。我认为我没有返回或没有正确传递变量。下面是代码。有人可以帮忙吗??

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

【问题讨论】:

    标签: python arrays oop object initializer


    【解决方案1】:

    你还没有定义 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']
    

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

    【讨论】:

    • 谢谢!但是我将数组放在类中,因为我必须创建: • 一个构造函数(Python 中的 def __init__(self)),它创建一个初始状态为 off 的 quizGrader。 • 将打开机器的设置方法。 • 一种初始化正确答案数组的set 方法。 • 一种初始化学生答案数组的set 方法。 • 对学生答案评分的工作方法。 • 获取正确答案数量的get 方法。 • 一种显示正确答案数量的工作方法。
    • 你应该设置类成员而不是试图在构造函数中传递它们
    • 感谢您的帮助。我修复了它,但仍然无法调用要评分的列表。
    • 'code' defgradeTest(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 =('学生的成绩是',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
    • 我也是新手,不知道怎么在评论区放代码。对不起。
    【解决方案2】:

    变量correct 未在您的main 函数中定义。

    例如,您可以简单地在grade = Answers(correct, student, theGrade) 行之前添加correct = 'C' 行,以表明正确答案是C

    成品看起来像这样:

    ...
    correct = 'C'
    grade=Answers(correct, student, theGrade)
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      相关资源
      最近更新 更多