Code and output are correct. Still error: "get_average(alice) raised the following error: string indices must be integers, not str"


#1



https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/2?curriculum_id=4f89dab3d788890003000096

  • Oops, try again. get_average(alice) raised the following error: string indices must be integers, not str


No error code. next lesson. I've checked the code in Python 3.5.2 IDLE No errors what so ever.
Please tell me what I'm doing wrong. I'd like to go on.


> 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]
> }

> students = [lloyd, alice, tyler]
> # Add your function below!
> def average(numbers):
>    total = sum(numbers)
>    return float(total) / len(numbers) 

> def get_average(students):
>     totAvg = 0
>     for student in students:
>         homework = average(student['homework'])
>         quizzes  = average(student['quizzes'])
>         tests    = average(student['tests'])
>         Avg = (0.1 * homework) + (0.3 * quizzes) + (0.6 * tests)
>         print ('%s has an average score of: %.2f' %  (student['name'], float(Avg)))
>         totAvg += Avg
>     return totAvg / len(students)

> print("Average over all students = %.2f" % (get_average(students)))


#2

This code is a complete train wreck no offence. it should look something like this:

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]
}

def average(numbers):
    total = sum(numbers) 
    total = float(total) / len(numbers)
    return total
    
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"


students = [lloyd, alice, tyler]

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

notice how the code explain itself. this is good coding cause i leave out all the unnecessary stuff. And i have to mention your code is completely open for errors to occur.


#3

Here is my code after completing the chapter. Looks very much like yours :slight_smile:
It's a pitty you have lecture in the way you do.

My problem was that misinterpreted the instructions at UNIT 6, paragraph 6 "Just Weight and See"
After I saw your code, I could adjust mine and continue and ultimately produce the very same code as you send me.:joy:

But, even though I initially didn't get any error messages in IDLE, AND
the "train-wrecked" program produced a correct output,
I did get an answer on what was wrong.

So why did I get that particular error message?

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]
}

def average(numbers):
    total = sum(numbers)
    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"

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

students = [lloyd, alice, tyler]
print get_class_average(students)
print get_letter_grade(get_class_average(students))

#4

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):
return (float(sum(numbers)))/len(numbers)

def get_average(student):
homework= average(student["homework"])
quizzes = average(student["quizzes"])
tests= average(student["tests"])
return (.1*homework)+(.3*quizzes)+(.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'

def get_class_average(student):
results = []
for item in student:
results.append(get_average(item))
return average(results)

print get_class_average([lloyd, alice, tyler])
print get_letter_grade(get_class_average([lloyd, alice, tyler]))

what is wrong in this ?

i get the error :
Oops, try again. One of the following is missing or broken when we tried to use it: alice, lloyd, tyler, students, get_class_average, get_letter_grade


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.