这个问题来自代码学院。我提出了一个解决方案,虽然不太满意,因为我认为我在程序上作弊使代码工作。还有其他方法可以解决这个问题。我见过更难的代码。这个很直接。
有关四个键的学生的三本词典。其中三个键是列表。 计算平均值的函数 函数来计算字母等级 计算每个孩子的平均值的函数
好的方面是,调用每个kid函数的平均值来计算平均值就像所有函数一样正常。
但是,如果创建一个新列表以包括所有3个学生 - 即学生列表,则包含3个列表项。每个项目都有一个字典,有四个键。四个键中的三个是列表。呼!!!
我使用带有计数器的for循环和getClassAverage函数的增量来计算平均值。
问题:
是否有另一种方法可以计算没有for循环的平均值,即只是在函数内,因为只使用“who”传递参数使得它具有学生(数据)并且不能很好地引用列表。使用字符串给出索引,而不是整数“错误?
使用列表内的键,内部列表。如何从getClassAverage函数中打印学生姓名?
谢谢!
Lloyd = {
"name":"Lloyd",
"homework": [90,97,75,92],
"quizzes": [ 88,40,94],
"tests": [ 75,90]
}
Alice = {
"name":"Alice",
"homework": [100,92,98,100],
"quizzes": [82,83,91],
"tests": [89,97]
}
Tyler = {
"name":"Tyler",
"homework": [0,87,75,22],
"quizzes": [0,75,78],
"tests": [100,100]
}
def average(stuff):
return sum(stuff)/len(stuff)
def getLetterGrade(score):
score = round(score)
if score >= 90: return "A"
elif 90 > score >= 80: return "B"
elif 80 > score >= 70: return "C"
elif 70 > score >= 60: return "D"
elif 60 > score: return "F"
def getAverage(kid):
bar = average
return bar(kid["homework"])*.1 + bar(kid["quizzes"])*.3 + bar(kid["tests"])*.6
students = [Lloyd,Alice,Tyler]
def getClassAverage(who):
class_ave = 0
class_avetot = 0
count = 0
for x in who:
class_avetot = class_avetot + getAverage(who[count])
#print who[count]
stu_score = getAverage(who[count])
print stu_score
print getLetterGrade(stu_score)
count = count + 1
class_ave = class_avetot / len(who)
return class_ave
print str(getClassAverage(students))
答案 0 :(得分:0)
为了估算平均值,numpy有一个你可以使用的功能。
from numpy import average
在getClassAverage函数中,您传递的是包含学生姓名的列表。 所以如果你这样做:
for x in who:
print x # This prints the name of the student
class_avetot = class_avetot + getAverage(who[count])
当你估计他的平均成绩加到班级平均水平时,它会打印学生的姓名。
希望有所帮助。
答案 1 :(得分:0)
使用您已有的代码和功能,试试这个:
def getClassAverage (who):
return average ( [getAverage (kid) for kid in who] )
你的代码很好。我将发布如何实现这一点,以便为您提供另一点,而不是说我的版本更好或更好:
#! /usr/bin/python3.2
def ave (x): return sum (x) / len (x)
def getLetterGrade (score):
score = round (score)
if score >= 90: return 'A'
if score >= 80: return 'B'
if score >= 70: return 'C'
if score >= 60: return 'D'
#Why no grade E?
return 'F'
class Student:
def __init__ (self, name, homework, quizzes, tests):
self.name = name
self.homework = homework
self.quizzes = quizzes
self.tests = tests
@property
def average (self):
return ave (self.homework) * .1 + ave (self.quizzes) * .3 + ave (self.tests) * .6
def printAverage (self):
print ('{} has an average of {:.2f} ({})'.format (self.name, self.average, getLetterGrade (self.average) ) )
class Class:
def __init__ (self, students):
self.students = students
@property
def average (self):
return ave ( [student.average for student in self.students] )
def printStudents (self):
for student in students:
student.printAverage ()
def printAverage (self):
print ('The class has an average of {:.2f}'.format (self.average) )
students = [Student ('Lloyd', [90,97,75,92], [88,40,94], [75,90] ),
Student ('Alice', [100,92,98,100], [82,83,91], [89,97] ),
Student ('Tyler', [0,87,75,22], [0,75,78], [100,100] ) ]
myClass = Class (students)
myClass.printStudents ()
myClass.printAverage ()
(Inb4 PEP8,我知道,我已经读过了,我选择忽略它。)