早教吧作业答案频道 -->其他-->
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,又有不懂了。这是...的网友还看了以下:
求解初一数学题有理数的加减混合运算(-3又三分之二)+(-2.4)-(-三分之一)-(-4又五分之 2020-04-09 …
第1题9又15分之1+99又15分之2+999又5分之1+9999又15分之4+99999又3分之 2020-05-23 …
下面我们来研究数学方面的问题有几条这样的条件“偶数又加偶数”为偶数;“偶数又乘奇数”为偶数“偶数又 2020-06-06 …
2道计算题,计算题51又3分之2÷3分之5+71又4分之3÷4分之7+91又5分之4÷5分之97又 2020-06-07 …
数学题一定要自己写出来还是多看几次知道怎么做就行了我买了一本题典,题目又多又难,我觉得根本没时间写 2020-06-10 …
第一题:14又九分之二÷4减(3+5÷9)第二题:一又十七分之四×(2又三分之二-0.75)加15 2020-07-14 …
"i是虚数单位"是什么意思?有些题目中会出现括号,里面写着(i是虚数单位),对题目又有什么提示呢? 2020-07-30 …
关于中国文化的语文题中国文化里有个很有趣现象.比方说:有“忍一时风平浪静”,又有“是可忍孰不可忍”; 2020-11-03 …
几道计算题,15又2分之1-4又12分之5-3又8分之31又3分之2+12又8分之1-(6又6分之5 2020-11-24 …
下面对有关名著名篇的说明,不正确的两项是A.《呐喊》以日记体的形式,纪录了“狂人”的心理活动;小说集 2020-12-06 …