Student becomes teacher | 4. for the record | print issue


#1



I do not understand what is wrong with this code. Can't I call the dictionary keys with the variable student in the code below.

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


Traceback (most recent call last):
File "python", line 29, in
TypeError: string indices must be integers, not str


lloyd = {
    "name": "Lloyd",
    "homework": [],
    "quizzes": [],
    "tests": []
}
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]
}
lloyd["homework"] = [90.0, 97.0, 75.0, 92.0]
lloyd["quizzes"] = [88.0, 40.0, 94.0]
lloyd["tests"] = [75.0, 90.0]
students = []
students.insert(0, "lloyd")
students.insert(1, "alice")
students.insert(2, "tyler")

"""" for loop to print the student's data starts here"""
for student in students:
    print student["name"] 
    print student["homework"]
    print student["tests"]
    print student["quizzes"]


#2

The students list should only contain the three object identifiers...

[lloyd, alice, tyler]

#3

This works fine.

Thanks.


#4

Sir, in the below snippet without calling the function how will it start executing the body.

def get_average(student):
total = sum(student['homework'])
homework = total/len(student['homework'])

total = sum(student['quizzes'])
quizzes = total/len(student['quizzes'])

total = sum(student['tests'])
tests = total/len(student['tests'])

final = 0.1*homework + 0.3*quizzes + 0.6*tests

**return final**

#5

The code in your post is hard to read but it looks like an indentation problem. Not sure.

total needs to be updated. Your code is replacing it.


Consider not having a total until the end summation.

    homework = average(student['homework']) * 0.1
    # &c.
    return homework + quizzes + tests

#6

My bad, I didn't paste it properly.
The fix worked.
Thanks.


#7

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