Python,又有不懂了。这是题目,只有英文的:Write a function average that takes
Python,又有不懂了。
这是题目,只有英文的:
Write a function average that takes a list of numbers and returns the average.
1. Define a function called average that has one argument, numbers.
2. Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total.
3. Like the example above, use float() to convert total and store the result in total.
4. Divide total by the length of the numbers list. Use the built-in len() function to calculate that.
5. Return that result.
注释后面是我自己写的:
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=0
for numbers in numbers:
total += numbers
return float(total) / len(numbers)
交上去提示错误:
object of type 'int' has no len()
请问应该怎么写才对?谢谢