【发布时间】:2018-02-28 00:51:23
【问题描述】:
我正在尝试修改我构建的考试平均值计算器,以在结束 while 循环后计算总体平均值(示例代码下方的所需输出)。我有功能代码,目前计算个别学生的平均值,但我一直无法找到计算整体平均值的方法。当前代码错误提示:TypeError: 'float' object is not iterable
numExams = int(input("How many exam grades does each student have? "))
students = 0
total = 0
moreGrades = "Y"
while moreGrades == "Y" :
print("Enter the exam grades: ")
total = 0
for i in range(1, numExams + 1) :
score = int(input("Exam %d: " % i))
total = total + score
average = total / numExams
print("The average is %.2f" % average)
moreGrades = input("Enter exam grades for another student? (Y/N)")
moreGrades = moreGrades.upper()
students = students + 1
total = sum(average)
print("The overall average is: ", total/students)
示例期望输出:
How many exam grades does each student have? (2)
Enter the exam grades.
Exam 1: (40)
Exam 2: (40)
The average is 40.00
Enter exam grades for another student? (Y/N) Y
Enter the exam grades
Exam 1: 20
Exam 2: 60
The average is 40.00
Enter exam grades for another student? (Y/N) n
The overall average is 40.00
【问题讨论】:
-
欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。您的缩进无效,我不确定您认为循环的底部应该在哪里。
标签: python for-loop while-loop sum average