早教吧 育儿知识 作业答案 考试题库 百科 知识分享

Python,又有不懂了。这是题目,只有英文的:Writeafunctionaveragethattakesalistofnumbersandreturnstheaverage.1.Defineafunctioncalledaveragethathasoneargument,numbers.2.Insidethatfunction,callthebuilt-insu

题目详情
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()
请问应该怎么写才对?谢谢
▼优质解答
答案和解析
你的numbers要求是一个数组啊>>> def average(numbers): total = sum(numbers) total = float(total) return total / len(numbers)>>> >>> average([1,2,3,4,5,100])19.166666666666668>>>