2

快速提问...我一直在开发一个小程序,该程序将根据“家庭作业”、“测验”和“测试”以及获得的字母成绩来计算最终成绩百分比。这是一个我正在测试新事物的程序,我想知道如何在课堂上或课堂内使用所有这些?

我考虑过保持功能性的东西不变,并且只使用一个类来打印出一个很好的学生姓名、最终字母等级和百分比的字符串。即使对于课堂内的简单打印,我也无法引入外部数据。有任何想法吗?

另外,有没有让用户的输入创建全新的空字典?(也许如果可以创建多个字典,我可以创建一个函数或方法来计算班级平均值)感谢您的帮助。谢谢!

student1 = {}
student = [student1]
def maker_d():
    name = raw_input("Please enter the student's name: ")
    student1['Name'] = name
    homework = raw_input("Please enter all of the student's homework grades separated by     a space: ")
    homework = [float(i) for i in homework.split()]
    student1['homework']= homework
    quizzes = raw_input("Please enter all of the student's quiz scores separated by a space: ")
    quizzes = [float(i) for i in quizzes.split()]
    student1['quizzes'] = quizzes
    tests = raw_input("Please enter all of the student's test scores separated by a space: ")
    tests = [float(i) for i in tests.split()]
    student1['tests'] = tests


def average(student):
    return sum(student) / len(student)

def get_average(student):
    h = average(student['homework'])
    q = average(student['quizzes'])
    t = average(student['tests'])
    total_average = (h * 0.1) + (q * 0.3) + (t * 0.6) #designates homework as 10%, quizzes as 30% and tests as 60% of final grade average
    return total_average


def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif 80<= score <90:
        return "B"
    elif 70 <= score < 80:
        return "C"
    elif 60 <= score < 70:
        return "D"
    else:
        return "F"


def get_class_average(student):
    total = 0
    for each in student:
        total += get_average(each)
    return total / len(student)
    return get_class_average(get_average[student])

    class FinalGradePrinter(object):


        def __init__(self,name):
            self.name = name
        def __str__(self,name):
            print "% ',' your final grade is a %s based on your overall percentage of %f" % (self.name) 



def main():
    maker_d()
    print get_class_average(student)
    print get_letter_grade(get_class_average(student))



if __name__ == '__main__':
    main()
4

1 回答 1

4

类是您要在应用程序中使用的对象的模板,对象旨在通过封装“事物”的属性来封装“事物”,以及使用这些属性的函数(例如,根据属性值计算值,更改它们等)。这个话题涉及的太多了,不能在这里给出更多的介绍,值得对面向对象的编程进行一些研究。

在您的情况下,您肯定有一个Student作为类的案例,这是您的代码中的一个“事物”,它具有属性,以及与这些属性一起使用的函数。下面是您创建自己的 Student 类的一个很好的起点 - 尝试填写该类并修改您的代码以使用它是一个很好的练习(这将需要学习面向对象的编程)。随着示例的发展,您可能还想为测验、测试和家庭作业创建类。

class Student:

    # Constructor
    def __init__(self, name, homework, quizzes, tests):
        self.name = name
        self.homework = homework
        self.quizzes = quizzes
        self.tests = tests

    # Function to return average for this student
    def get_average(self):
        h = average(self.homework)
        ...
        return total_average

祝好运并玩得开心点!

于 2013-07-03T08:35:40.660 回答