8. Part of the whole - string indices must be integers, not str


#1



8. Part of the whole - string indices must be integers, not str


Traceback (most recent call last):
File "python", line 53, in
File "python", line 50, in get_class_average
File "python", line 28, in get_average
TypeError: string indices must be integers, not str


Cant see where the type issue is coming from


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

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

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  results 
    
print average(get_class_average(student))


#2

Read last line of instructions.

Finally, return the result of calling average() with results.

Make changes in your get_class_average() function.

Also you've not defined anything like student so it's(variable) alien to python so It will throw an error.


#3

Thank you, I have changed accordingly and now get a numerical answer (83.8666666667) however it is the wrong answer, I have seen this in other threads so will have a look there unless its clear whats wrong :slight_smile:


#4

Can you put updated code ?


#5

Output:
83.8666666667
None

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

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

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  results 
    
print average(get_class_average(students))

#6

The final object that we're returning here is nothing but a list (Current code), results is a list. (in get_class_average() function)
But we need to take average of numerical values inside of results list and return it ?
We already defined a function to do this for us? (average() function)

Note:
Take a look at instructions and theory.
Specially check last line of instructions.


#7

I believe I am already doing this:

print average( get_class_average(students) ) - last line

It must be finding the average of something otherwise it would be printing a list of three


#8

There is a problem, CC does not just check for one case that you passed there but will automatically check it on multiple calls by passing different argument.
so you need to define your get_class_average() function so that it returns average of list instead of list.

You need to call average() function with results argument and then return it? (inside of get_class_average())


#9

I moved the average function into the get_class_average function however,

The code still outputs:
83.8666666667
None

Although the "checker" now says its correct?

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

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

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))

#10

I hope,I have not overlooked anything, Just tested it on students argument and it gives me
83.8666666667 and lets me pass.

also when I deleted final print code, I get None and it lets me pass too.
Try refreshing and submit again ?(or deleting the final print code?)


#11

Thank you for your help - I assume 83.866... is the right answer so all is good :slight_smile:


#12

If exercise did not let us pass, we need to take a look at code again!


#13

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