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