Jump to content

"List indecies must be int not str" -Python

Go to solution Solved by flexin1981,

hey I think that the issue is that you are passing in the whole list to the "get_average(student)" function, try the following;

def get_class_average(students):    results = []    for stud in students:        results.append(get_average(stud))

 I'm having an issue with part of the CodeCademy Python course. Specifically to do with adding things to dictionaries.

 

In the example below, running it in CodeCademy to see if it's right results in "List indecies must be integers, not strings". I take this code into IDLE and if i put get_class_average(alice) it returns a number, as expected. But if I put get_class_average([alice]) as CodeCademy inputs, I get the error.

 

 

http://i.imgur.com/bp8cJfF.png <-- an image showing the code running in both CodeCademy and IDLE.

 

lloyd = {
    "name": "Lloyd",
    "homework": [90, 97, 75, 92],
    "quizzes": [88, 40, 94],
    "tests": [75, 90]
}
alice = {
    "name" : "Alice",
    "homework" : [100, 92, 98, 100],
    "quizzes" : [82, 83, 91],
    "tests" : [89, 97]
}
tyler = {
    "name": "Tyler",
    "homework": [0, 87, 75, 22],
    "quizzes": [0, 75, 78],
    "tests": [100, 100]
}

# Add your function below!
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"




get_letter_grade(get_average(lloyd))


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

 

I have no idea what's gone wrong here. Another student in my class is also stuck at this point. Our teacher is currently off sick.

 

Help appreciated, ty!

 

     ~pipnina

Link to comment
https://linustechtips.com/topic/297954-list-indecies-must-be-int-not-str-python/
Share on other sites

Link to post
Share on other sites

hey I think that the issue is that you are passing in the whole list to the "get_average(student)" function, try the following;

def get_class_average(students):    results = []    for stud in students:        results.append(get_average(stud))
Link to post
Share on other sites

 

hey I think that the issue is that you are passing in the whole list to the "get_average(student)" function, try the following;

def get_class_average(students):    results = []    for stud in students:        results.append(get_average(stud))

 

Oh my gosh. That solved it!

 

Thanks so much i've been stuck on this for ages.

 

I'll now have to tell my classmate this as well. We must have been stuck in the same spot.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×