How is Everybody Doing?


#1


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

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


 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)
    totty = total / len(numbers)
    return totty
    
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 stu in students:
        results.append(get_average(stu))
    return average(results)
 
   
print get_class_average(students)
print get_letter_grade(get_class_average(students))


#2

Ok so I just looked over your code and I just copy and pasted your whole code into my lesson browser and it passed and looked fine. Try refreshing or using a different internet browser?


#3

I tried to refresh it, copied/pasted in different browsers, It's still throwing an error.


#4

I am also a beginner, but is it possible you need to first create a list called "students" before it can be used as a parameter in your functions?

example:
students = [lloyd, alicia, tyler]

I hope this helps.


#5

Tried this too, still throwing an error.
But I think its already defined, the list students.


#6

The first code you posted works if you define the students list like this:


#7

I'll try to help without giving you the answer.

print get_class_average(students)
print get_letter_grade(get_class_average(students)) <-------

Remember you've already defined a "get_average" function with a parameter (student) to calculate the weighted average of each "homework", "quizzes", "tests". You don't need to call and print the results of "get_class_average" function twice.

You're trying to call "get_class_average" function with the results of "get_letter_grade". When the "get_class_average" function used your "get_average" function to return it's results.

So you want to print the results of your "get_letter_grade" which was calculated by using all 3 "homework, "quizzes", "tests" weighted averages.

I hope this helps. I'm no teacher, and still learning myself.


#8

yes it is working create a list student at the first line of code
students = [lloyd, alice, tyler]
now its working perfectly


#9

had the same problem

refreshed my browser shaken few errors away
then 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]

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    total = total / len(numbers)
    return total
def get_average(student):
    homework = average(student['homework'])
    quizzes = average(student['quizzes'])
    tests = average(student['tests'])
    sum = homework* 0.10 +   quizzes* 0.30 +  tests* 0.60
    return sum
    
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))

ps. defined the student list in the top right after the dictionaries


#10

I think stu change student

Example:
for student in students:
results.append(get_average(student))


#11

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