代码学院:每个人都在做什么?

时间:2016-05-14 18:11:04

标签: python

我在CodeAcademy第9课("每个人都在做什么?") 我的错误消息如下:

Traceback (most recent call last):
  File "python", line 60, in <module>
  File "python", line 52, in get_class_average
  File "python", line 28, in get_average
TypeError: string indices must be integers, not str

我不知道该怎么做,我已经搜索了解决方案,他们建议我使用变量q,我把它放在我的代码中,但问题似乎是我已经存在的问题以前课程中的代码。

 # creating students
 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 = sum(numbers)
   total = float(total)
   return float(total)/ len(numbers)

def get_average(student):
   homework = average(student["homework"])
   quizzes = average(student["quizzes"])
   tests = average(student["tests"])
   return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

def get_letter_grade(score):
   if score >= 90:
       return "A"
   elif score >= 80:
       return "B"
   elif score >= 70:
       return "C"
   elif score >= 60:
       return "D"
   else:
       return "F"

 #summarising students to classlist
 students = ["lloyd", "alice", "tyler"]

 # calculating class average        
def get_class_average(students):
   results = []
   for student in students:
       results.append(get_average(student))
   return average(results)

 #printing grades       
print get_letter_grade(get_average(lloyd))
print get_letter_grade(get_average(alice))
print get_letter_grade(get_average(tyler))

q = get_class_average(students)
#printing class average
print get_letter_grade 
print q

2 个答案:

答案 0 :(得分:3)

get_class_average中,您使用get_average列表中的对象调用students。但是,这些对象是字符串。相反,您可能想要之前定义的变量。所以替换

students = ["lloyd", "alice", "tyler"]

students = [lloyd, alice, tyler]

答案 1 :(得分:0)

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 = sum(numbers)
    total = float(sum(numbers))
    return total/len(numbers)
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average (student["tests"])
    return homework*.10 + quizzes*.30 + tests*.60
def get_letter_grade(score):
    if score >=90:
        return "A"
    elif score >=80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"

print get_letter_grade(get_average(lloyd))
def get_class_average(students): 
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)

print get_class_average(students)
print get_letter_grade(getclassaverage(students))