我正在为我的CS课程编写一个程序来计算成绩的加权平均值,但在尝试运行它几次之后,我继续得到这个奇怪的内置错误。我不知道问题是什么,因此很有可能在我的其他一些定义中出现同样的问题。
def main():
#user inputs grades into a set of lists
assignmentList = [float(input('assignment scores: ')) for grade in range(14)]
quizList = [float(input('quiz scores: ')) for grade1 in range(4)]
wExamList = [float(input('written exam scores: ')) for grade2 in range(2)]
lExamList = [float(input('lab exam scores: ')) for grade3 in range(2)]
finalExam = [float(input('final exam score: ')) for grade4 in range(1)]
print()
#import the definitions
gradeAvg = round(assignments(assignmentList) + quizzes(quizList) + writtenExams(wExamList) + labExams(lExamList) + finalExam(finalExam), 2)
print('Overall grade:', gradeAvg)
#create elifs to figure out Letter grades
if gradeAvg <= 93:
print('Letter grade: A')
elif 90 <= gradeAvg > 93:
print('Letter grade: A-')
elif 87 <= gradeAvg >89:
print('Letter grade: B+')
elif 83 <= gradeAvg > 87:
print('Letter grade: B')
elif 80<= gradeAvg > 83:
print('Letter grade: B-')
elif 77 <= gradeAvg >80:
print('Letter grade: C+')
elif 73 <= gradeAvg >77:
print('Letter grade: C')
elif 70 <= gradeAvg >73:
print('Letter grade: C-')
elif 67<= gradeAvg >70:
print('Letter grade: D+')
elif 60 <= gradeAvg >67:
print('Letter grade: D')
else:
print('Letter grade: F')
def assignments(assignment):
list1 = [assignment]
#sort assignments and delete the lowest score
list1.sort()
del list1[0]
#get the weighted average for homework
assignAvg = sum(list1)
assignAvg = round((assignAvg / 14) * .25, 2)
return assignAvg
def quizzes(quiz):
list2 = [quiz]
list2.sort()
#weighted average for quizzes
quizAvg = (sum(list2))
quizAvg1 = round((quizAvg / 4) * .15, 2)
return quizAvg1
def writtenExams(wExam):
list3 = [wExam]
#weighted average for written exams
examAvg = sum(list3)
examAvg1 = round((examAvg / 2) * .20, 2)
return examAvg1
def labExams(lExam):
list4 = [lExam]
examAvg = sum(list4)
examAvg2 = round((examAvg / 2) * .20, 2)
return examAvg2
def finalExam(fExam):
list5 = [fExam]
examAvg = sum(list5)
examAvg3 = round(examAvg * .20, 2)
return examAvg3
main()
错误的第一个实例是第66行,其中'quizAvg =(sum(list2))'
答案 0 :(得分:3)
quizList
是list
。您将其传递给quizzes()
。然后你做list2 = [quiz]
。这不仅不会生成quiz
的单独副本,而是将其包装在另一个list
中。你会得到这样的东西:
[[1, 3, 3, 2, 4]]
这意味着当您将其发送到sum()
时,它会尝试将第一个元素添加到默认的起始值0,这意味着它在内部执行0 + [1, 3,...]
- 并且它在那里添加了{ {1}}和int
。
相反,请单独制作一个副本进行排序,不要更改结构:
list