Part of the Whole


#1

https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/4

I am getting the following error message:
Oops, try again. get_class_average([alice]) resulted in an error: string indices must be integers, not str

My code is as follows:

l = sum(numbers)
    total = float(total)
    total = total / len(numbers)
    return total
print average([5,4,6,111,123])

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    weighted = .1 * homework + .3 * quizzes + .6 * tests
    return weighted

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)])
print get_letter_grade([get_average(tyler)])
print get_letter_grade([get_average(alice)])

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

#2

You have a typo in the penultimate line of code.


#3

In other words, where is the definition line for your average function?


#4

I mean this line has a typo:

reults.append()

r e u l t s is not a word


#5

That would have been the answer to give. It's hard enough to get learners to search and read docs. Adding a dictionary to that is only going to add to confusion. Sometimes the simplest language is best... 'next to the last line'.


#6

Thanks for the feed back. I changed reults to results. I am now getting the error:Oops, try again. get_class_average([alice]) resulted in an error: string indices must be integers, not str.

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

Updated code:

class_list = ["lloyd", "alice", "tyler"]
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]
}

def average(numbers):
    total = 0
    total = sum(numbers)
    total = float(total)
    total = total / len(numbers)
    return total
print average([5,4,6,111,123])

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    weighted = .1 * homework + .3 * quizzes + .6 * tests
    return weighted

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)])
print get_letter_grade([get_average(tyler)])
print get_letter_grade([get_average(alice)])

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

#7

The class list is supposed to be named, students and should consist of identifiers, not strings.

students = [lloyd, alice, tyler]

To be sure it references defined objects, the list should follow the dictionaries, not precede them.


#8

I made the corrections. I am getting the following error message now:"Oops, try again. get_class_average([alice]) resulted in an error: 'list' object has no attribute 'average'"

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

updated 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]
}
students = [lloyd, alice, tyler]

def average(numbers):
    total = 0
    total = sum(numbers)
    total = float(total)
    total = total / len(numbers)
    return total
print average([5,4,6,111,123])

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    weighted = .1 * homework + .3 * quizzes + .6 * tests
    return weighted

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)])
print get_letter_grade([get_average(tyler)])
print get_letter_grade([get_average(alice)])

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

#9

3 posts were split to a new topic: Appreciate some guidance


#14

Why is that? Why the return statement shouldn't be inside to loop? I wonder this thanks in advance.


#16

On the first iteration of the for loop, it will execute the return statement and leave the function.

In this example, the loop needs to run three times before exiting the loop and running the return statement. Anything that is indented after the for loop will iterate the number of times the loop is set to... well loop. Removing the indentation on your return statement will allow all three averages to be appended to the results list.

Ask away if there are still any doubts. Happy coding!


#19