0
所以這裏是我的python代碼到目前爲止。我想使用這些字典並將它們製作成學生名單。我無言以對,如何把它們運行到getAverage()
功能,產生這種結果函數如何取字典參數
Name: Alice
Homework: [100.0, 92.0, 98.0, 100.0]
Quizzes: [82.0, 83.0, 91.0]
Tests: [89.0, 97.0]
For Alice the average is:91.14999999999999
Name: Lloyd
Homework: [90.0, 97.0, 75.0, 92.0]
Quizzes: [88.0, 40.0, 94.0]
Tests: [75.0, 90.0]
For Lloyd the average is:80.55
Name: Tyler
Homework: [0.0, 87.0, 75.0, 22.0]
Quizzes: [0.0, 75.0, 78.0]
Tests: [100.0, 100.0]
For Tyler the average is:79.9
這是代碼我迄今所做的:
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]}
rich = {
"name": "Rich",
"homework": [95.0, 93.0, 81.0, 94.0],
"quizzes": [88.0, 55.0, 77.0],
"tests": [80.0, 95.0]}
josh = {
"name": "Josh",
"homework": [93.0, 94.0, 74.0, 99.0],
"quizzes": [87.0, 47.0, 92.0],
"tests": [70.0, 88.0]}
def average(n):
count = float(len(n))
total = float(sum(n))
return total/count
def getAverage(**names):
homework = average(names[homework])
quizzes = average(names[quizzes])
tests = average[names[tests]]
return float((.1*homework +.3*quizzes + .6*tests)/3)
students = [rich, josh, lloyd]