修改平均计算器-Python

修改平均计算器-Python,python,for-loop,while-loop,sum,average,Python,For Loop,While Loop,Sum,Average,我正在尝试修改一个考试平均值计算器,它是我在结束while循环后用来计算总体平均值的(代码下面的示例所需输出)。我有一个功能代码,目前计算每个学生的平均数,但我还没有找到一种方法来计算总体平均数。当前代码错误为:TypeError:“float”对象不可编辑 numExams = int(input("How many exam grades does each student have? ")) students = 0 total = 0 moreGrades = "Y" while

我正在尝试修改一个考试平均值计算器,它是我在结束while循环后用来计算总体平均值的(代码下面的示例所需输出)。我有一个功能代码,目前计算每个学生的平均数,但我还没有找到一种方法来计算总体平均数。当前代码错误为:TypeError:“float”对象不可编辑

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

那是因为你在努力

total = sum(average)
您可以将
average
定义为
while
循环之外的

average = []

因此,它是一个列表,您可以在
循环中附加每个平均值。请参阅文档。

您试图对浮点数求和,这是不可能的:

>>> sum(40.00)
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    sum(40.00)
TypeError: 'float' object is not iterable
代码输出:

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.0

欢迎来到StackOverflow。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。你的缩进无效,我不确定你认为循环的底部应该在哪里。我没有正确地将缩进粘贴到这里,所以我知道缩进错误,但列表就是我所需要的!我本想用一个,但我是初学者,所以我忘了。
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.0