简体   繁体   English

如何使用Python查找平均值?

[英]How can I find the average of averages using Python?

I have to find the average scores of a class of students. 我必须找到一班学生的平均分数。 I want to do this by finding an average of each student and finding the average of those averages. 我想通过找到每个学生的平均值并找到这些平均值的平均值来做到这一点。 I have everything else in the program working perfectly, except for this block: 除了以下代码块,我在程序中的所有其他内容均正常运行:

def get_class_average(class_list): total = 0 for student in class_list: student = student["name"] get_average(student) total_class += get_average(student) def get_class_average(class_list):班级列表中学生的总数= 0:student =学生[“名称”] get_average(学生)total_class + = get_average(学生)

I can give you the rest of the code if someone asks, but I know it's probably that paragraph. 如果有人询问,我可以为您提供其余的代码,但是我知道这可能就是该段。 Here's my error: 这是我的错误:

get_class_average([lloyd]) returned 85.85 instead of 80.55 as expected get_class_average([lloyd])返回85.85而不是预期的80.55

So I'm doing something mathematically incorrect. 所以我在做数学上不正确的事情。 I believe the problem is that I'm trying to do this with a for loop. 我相信问题是我正在尝试使用for循环来做到这一点。 How would you find the correct average of an entire class? 您如何找到整个班级的正确平均值?

edit: 编辑:

Here is another function I have in the code. 这是我在代码中拥有的另一个功能。 It's not the only other one, but it is where my error says it would be: 它不是唯一的一个,但是我的错误表明这是:

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    total /= len(numbers)
    return total

I think there's an error with the function for that reason. 我认为该功能存在错误。

Here is the whole code in case you want to look at it: 这是完整的代码,以备您查看:

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

results = []

# Add your function below!
def get_average(numbers):
    for total in numbers:
        total += total
        total *= 1.0
    total /= len(numbers)
    return total

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"

def get_class_average(class_list): total = 0 for student in class_list: student = student["name"] get_average(student) total_class += get_average(student) def get_class_average(class_list):class_list中学生的总数= 0:student = student [“ name”] get_average(学生)total_class + = get_average(学生)

return average(class)

Without knowing your functions it's a little hard to debug. 在不了解您的功能的情况下,调试起来有些困难。 I'm also wondering why you have a function get_average AND a function average . 我也想知道为什么你有一个函数get_average和一个average函数。 Anyway, I bet the error is that you return average(b) in your first code example as opposed to average(results) . 无论如何,我敢打赌,错误是您在第一个代码示例中返回了average(b)而不是average(results) Also, this statement would need one less level of indentation, or else your for loop will never go beyond the first x in students . 同样,此语句将需要较少的缩进级别,否则您的for循环将永远不会超出students的第一个x Currently, you are just returning average(get_average(x)) where x is the first item in students . 当前,您只是返回average(get_average(x)) ,其中xstudents的第一项。

Your second function is closer, the final return statement just needs one level less of indentation and you need to initialize results . 您的第二个函数更接近,最终的return语句只需要缩进一级,就需要初始化results Try this: 尝试这个:

def get_class_average(students):
    results = [] # initialize results
    for result in students:
        result = get_average(result)
        results.append(result)
    return average(results) # not part of the for looop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到同一年所有分数的平均值(Python) - How can I find the average of all scores with the same year (Python) 如何在不使用 sum() 的情况下从每个键中有多个值的字典中的平均值计算平均值? - How do I calculate an average from averages in a dictionary with multiple values in each key without using sum()? Python - 使用 TSV 文件求平均值 - Python - using TSV files to find averages Python:如何按类别拆分值和查找平均值 - Python: How to split values and find averages by category 在具有多个系统读数的pandas DataFrame中,如何计算每日平均值并选择每个系统的最新平均值 - In a pandas DataFrame with readings for multiple systems, how can I calculate daily averages and select the most recent average for each system 如何找到平均值? - How to find averages? 使用此代码在python中使用“ while”循环时,如何找到平均值? - how would i find the average when using a 'while' loop in python using this code? 我如何在python中平均一组数组? - How can I average an array of arrays in python? 必须定义一个 function 可以找到 python 中多个列表列表的平均值 - Have to define a function that can find the averages of multiple list of list in python 使用fsolve查找两个平均值在Python中相交所需的价格 - Using fsolve to find the price necessary for two averages to intersect in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM