Part of the whole-8


#1

Continuing the discussion from 8. part of the whole:

what's wrong with my 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)
return total / len(numbers)

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"

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


#2

@phoebelinane,
As =indentation= is very important in Python-code
Please

edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#3
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)
    return total / len(numbers)
    
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"

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

#5

Hi, maybe you should fix for loop in get_class_average

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

#6

when i put this code, it comes up saying "Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected"

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)
    return total / len(numbers)
    
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"

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

#8

hello phoebelinane,

You made a mistake while indenting in the get_class_average where the last statement for return average(results) should be not indented inside the for loop

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

Hope this helps :grinning:


#9

return statement is inside for loop, place it outside


#10

@saigoodguy08,

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#11

This is just a more general comment, in case anyone else was puzzled like me!
I think 8/9 would be easier to follow if the "students" list that you're asked to do in section 2/9 remains (or is replaced) in the code after the dictionaries. Although you're told in the instructions to assume "students" is a list containing the dictionaries, it would be more straightforward if it was there in the code. As it appears on screen, the for loop is going through a non-existent list!


#12

@kwlitf,
Maybe you could have explained
the terms

  • =dictionaries= ( an example ?? )
  • =a list containing the dictionaries= ( an example ?? )

as we are answering the questions of =real= beginners/learners


#13

Sorry. My previous comment was genuinely meant to be helpful. I'm a beginner too, only been learning for a couple of months so maybe my explanations are lacking somewhat! Here's my attempt to clarify what I was going on about!

~The term "dictionaries" is described earlier in the python course so I assumed this term was common knowledge.
~In the code below the "dictionaries" are lloyd, alice, tyler. They contain information as keys and associated values and are enclosed in { }
~The term "list" is also previously described earlier in the course, as a recap, lists are collections of data stored within square brackets [ ]. The list I was referring to was:
students = [lloyd, alice, tyler] ~this is an example of a list containing the previously defined dictionary names.

Here's the section of code from screen 3/9:

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]
*~This is the list I was referring to, it gets removed by the course in later sections.

~I simply felt that for exercise 8/9, since the for loop refers to this list, it would be helpful for it to remain on the code screen. I realised after posting that you have to add this list back in to the code to complete the next screen, 9/9. I hope this better explains what I was going on about!


#14