Codecademy上的python:求average(平均数)。求教如何修正?
PHP中文网
PHP中文网 2017-04-17 15:03:06
0
1
515

It's Okay to be Average

这里是需要从dictionaries中求平均数,详情点击

codecademy的instruction:

0.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.
Inside that function, call the built-in sum() function with the numbers list as a parameter. Store the result in a variable called total.
2.use float() to convert total and store the result in total.
3.pide total by the length of the numbers list. Use the built-in len() function to calculate that.
4.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):
numbers = [lloyd]
sum(numbers):
total = 0
total += total
float(total)
aver = total / len(numbers)
return aver

求教各位,这段代码该如何修改才是正确的?
谢谢大家!!

PHP中文网
PHP中文网

认证0级讲师

répondre à tous(1)
左手右手慢动作

这个instruction写得很清晰了其实...

代码实现是:

def average(numbers):
    total = sum(numbers)
    total = float(total)
    total /= len(numbers)
    return total
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!