【问题标题】:Amending Average Calculator - Python修正平均值计算器 - Python
【发布时间】: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


【解决方案1】:

那是因为你在努力

total = sum(average)

您可以在while 循环之外将average 定义为

average = []

所以这是一个列表,您可以将 while 循环中的每个平均值附加到此列表中。请参阅sum 的文档。

【讨论】:

    【解决方案2】:

    你试图总结一个浮点数,这是不可能的:

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

    您的代码也有一些缩进错误,这是您的代码的工作版本:

    numExams = int(input("How many exam grades does each student have? "))
    students = 0 
    total = 0
    moreGrades = "Y"
    average = [] # set average to an empty list
    
    while moreGrades == "Y" :
        print("Enter the exam grades: ")
        total = 0 
        for i in range(1, numExams + 1) : # you have indentation error here
            score = int(input("Exam %d: " % i))
            total = total + score
    
        average.append(total / numExams)
        print("The average is %.2f" % average[-1])
    
        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.0
    

    【讨论】:

    • 我没有正确缩进粘贴到这里,所以我知道缩进错误,但我只需要这个列表!我应该考虑使用一个,但我是初学者,所以我没有想到。
    猜你喜欢
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多