PYTHON:为什么" varaible = float(variable)/ len(variable2)" work,但" variable = float(variable)variable / len(variable2)"不起作用?

时间:2016-11-28 20:04:19

标签: python dictionary

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]
}

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

test = lloyd['homework']

average(test)`

当我测试平均值时,它不会像变量= float(变量)/ len(变量2)那样返回

抱歉我的格式不好

1 个答案:

答案 0 :(得分:1)

您应该返回total/len(numbers)。实际上,您计算平均值,但是不对该值执行任何操作,因此将其丢弃。

def average(numbers):
    total=sum(numbers)
    total=float(total)
    avg = total/len(numbers)
    print(avg)
    return avg