9. How is Everybody Doing? string indices must be integers, not str


#1

Hi pals, i've got the following error on my code and i can't figure out why it's wrong :
- string indices must be integers, not str -

What's my mistake?

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)
    total = float(total)
    total = total / len(numbers)
    return total
    
def get_average (students):
    homework = 0.1 * average(students["homework"])
    quizzes = 0.3 * average(students["quizzes"])
    tests = 0.6 * average(students["tests"])
    weights_average = homework + quizzes + tests
    return weights_average
    
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)

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

Thanks in advance


#2

Your get_average function takes a dictionary as input:

print get_average(tyler) # prints Tyler's average

In you students list there are three strings not dictionarys. You can't use the get_average function on a string:

print get_average("tyler") # causes an error

Change the students list to contain the dictionarys and it should work.

Doesn't change the functionality, but giving the parameter of get_average the name students is confusing because get_average works on one student at a time not multiple ones.


#3

Did you figure out the indicies error. Remove the " quotes


#4

The error I was talking about is the indices error.

Take a look at get_average("tyler")

The first line of get_average is:

homework = 0.1 * average(students["homework"])

So students["homework"] with "tyler" as the students parameter is "tyler"["homework"]
this look like you are trying to get the character in the string "tyler" that is a t the index "homerwork", but indexes are integers not strings.


#5

This code worked for me:
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]

for name in students:
print name["name"]
print name["homework"]
print name["quizzes"]
print name["tests"]

Well, for "student becomes the teacher" - your code is a different exerscise, i believe.


#6

I am with the exact same problem as you. The problem was "When you enclose a name in quotes, it is a string literal, not a variable." So should change to students= [lloyd, alice, tyler] instead of with the quote. But after this changing, a new alarm appears : TypeError: 'float' object is not callable


#7

Thanks everyone, i perfectly understand the problem !


#8

Welcome!:smiley::neutral_face:


#9

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