快速提问...我一直致力于一个小程序,根据“家庭作业”,“测验”和“测试”以及所获得的字母等级来计算最终成绩百分比。这是一个我正在测试新东西的程序,我想知道如何在课堂上或课堂内使用所有这些东西?
我已经考虑过保持功能性的东西,只是使用一个类来基本打印出一个很好的学生名字,最终字母等级和百分比字符串。即使是在课堂上进行简单的打印,我也无法引入外部数据。有什么想法吗?
另外,有没有让用户的输入创建全新的空字典? (也许如果可以创建多个词典,我可以创建一个函数或方法来计算类平均值) 我感谢您的帮助。 谢谢!
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()
答案 0 :(得分: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
祝你好运,玩得开心!