我写了下面的代码,并且不断出错。谁能帮我发现错误?函数roundGrade是我在代码上方拥有的一个函数,并且工作正常。
我想找到数字的平均值(最终等级),但有一些条件。有N个学生,他们已经分配了M个作业。如果M = 1,则最终成绩应为该作业的成绩。如果M> 1,则应除去最低等级,最终等级应为等级的平均值。如果其中一个等级为-3,则最终等级应为-3。这三个条件应该适用于我的代码。
def computeFinalGrades(grades):
g = grades
g2 = np.zeros(np.size(g[:,1]))
for i in range(np.size(g2)):
g3 = roundGrade(g[i,:])
if np.any(g3) == -3:
grade = -3
elif np.size(g3) == 1:
grade = g[0]
elif np.size(g3) > 1:
g4 = heapq.nlargest(np.size(g-1),g3)
grade = roundGrade(np.mean(g4))
else:
print("Invalid input, please try again")
g2[i] = grade
gradesFinal = g2
return gradesFinal
x = np.array([[1,2,-3,6],[2,3,9,0],[-3,4,8,7]])
print(computeFinalGrades(x))
File "C:/Users/Ambro/Desktop/project2grades.py", line 50, in computeFinalGrades
grade = roundGrade(np.mean(g4))
File "C:/Users/Ambro/Desktop/project2grades.py", line 14, in roundGrade
for i in grades:
TypeError: 'numpy.float64' object is not iterable