早教吧作业答案频道 -->其他-->
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()
请问应该怎么写才对?谢谢
这是题目,只有英文的:
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>>>
看了Python,又有不懂了。这是...的网友还看了以下:
已知关于X的一元二次方程x^2+2(k-1)x+k^2-1=0有两个不相等的实数根已知关于x的一元 2020-05-16 …
怎么排列把1、1、2、2、3、3、4、4,才能使两个1中间有1个其他数;两个2中间有2个其他数;两 2020-05-16 …
已知函数f(x)=|x|/(x+2),f(x)=kx^2有四个不同的实数解,求k的取值范围(急!) 2020-05-22 …
(X^2+Y^2-1)^2=4,X^2+Y^2=?有个疑问,用直接开方法求出的有两个结果3或-1. 2020-06-04 …
六年级下册数学难题数学广角(2)1.在一副52张扑克牌的扑克中,一次至少要拿多少张,才能保证4张花 2020-06-10 …
一元二次方程与根的分布实数a为何值时,方程(a-2)x^2-2(a+3)+4a=0有一根大于3,而 2020-06-23 …
在整数中,有用2个以上的连续自然数的和来表达一个整数的方法.例如9:9=4+5,9=2+3+4,9 2020-06-27 …
鼻腔前部生有,它有作用;鼻腔内表面有,可以分泌,黏液有和作用;黏膜中还分布鼻腔前部生有,它有作用; 2020-07-06 …
给出下列各数:1.414213,0,0.2(0.2的2头上有个点),3兀,22/7,³√2,0.1 2020-07-13 …
关于等差数列的两个问题在三角形ABC中,acos^C/2+ccos^A/2=3/2b.求证abc成 2020-07-20 …