How is Everybody Doing?


#1



I don't know what's wrong with my code.......


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

Oops, try again. One of the following is missing or broken when we tried to use it: alice, lloyd, tyler, students, get_class_average, get_letter_grade


Replace this line with your code. 
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)
    total = total/len(numbers)
    return total
    
def get_average(student):
    homework = average(student["homework"])#28#
    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:
        avg = get_average(student)#52#
        results.append(avg)
    return average(results)
    

students = ["lloyd", "alice", "tyler"]
print get_class_average(students)#57#
print get_class_average(get_letter_grade(students))


#2

lloyd alice and tyler don't need to be in quotes. Try unquoting them and see if that fixes the problem. I believe this is what it is referring to when it states that string indices must be integers not strings


#3

It 's OK~~ Thank you!!!
my code can be executed now


#4

No problem! Happy to help and now you can move on to 12.) List and Functions so have fun! :smiley:


#5

It disturbed me for an hour.......


#6

Yeah it happens. Sometimes you just get stuck....If you are trying to figure it out on your own usually googling an error can bring up other examples from coding forum sites that can help you determine your error to help you solve your code. But usually if you are confused this is the best place to come and we are more than happy to help you figure it out and learn what to do to fix it and pass the lesson !!! :thumbsup:


#7

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

def get_letter_grade(score):
score==float
if score >=90:
return "A"
elif score>=80:
return "B"
elif score>=70:
return "C"
elif score>=60:
return "D"
else:
return "F"
get_average(lloyd)
def get_class_average(students):
results = []
for student in students:
avg = get_average(student)
results.append(avg)
return average(results)
students = [lloyd, alice, tyler]
print get_class_average(students)#57#
print get_class_average(get_letter_grade(students))

still showing error
ate my 3 hours


#8

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

def get_letter_grade(score):
score==float
if score >=90:
return "A"
elif score>=80:
return "B"
elif score>=70:
return "C"
elif score>=60:
return "D"
else:
return "F"
get_average(lloyd)
def get_class_average(students):
results = []
for student in students:
avg = get_average(student)
results.append(avg)
return average(results)
students = [lloyd, alice, tyler]
print get_class_average(students)#57#
print get_class_average(get_letter_grade(students))


#9

Hi.
Under the get_average(student) function, why don't you use the variables you have created to "return" the average. I.E return 0.1 * homework + 0.3 * quizzes + 0.6 * tests.

Under get_letter_grade(score) function, you can remove this "score == float" as it is not necessary.

This is only to help clean up the code, it might help sort out your problem. I will try and run your code with the above suggestions and see if it works.


#11

Another correction is to use the print command under the get_letter_grade(score) function at the bottom using "lloyd" as an argument instead of calling the get_average(lloyd) function as you did. Use it as follows: print get_letter_grade(get_average(lloyd))


#12

Hi. Below is your code as it should be. The main error of string indices was caused by the last print statement. It should be called as follows with get_letter_grade() being the main function while the get_class_average() is used as the argument.

It should be this "print get_letter_grade(get_class_average(students))"

Instead of this "print get_class_average(get_letter_grade(students))"

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)
result = float(total)/len(numbers)
return result

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"

print get_letter_grade(get_average(lloyd))

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

students = [lloyd, alice, tyler]

print get_class_average(students)#57#
print get_letter_grade(get_class_average(students))


#13

Hi. Just curious why they didn't have to be in quotes. By putting them in quotes, they are strings, but by not putting them in quotes, what are they exactly? What area of the code would that be calling? I'm a bit confused by this.

Thanks.


#14

Hi. They are dictionaries which are at the beginning of your code which you put into a list called "students". Once you put them in quotes, then you are not adding the dictionaries to the list but you are only adding strings.

Hope this helps.


#15

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