How is Everybody Doing?


#1

I am trying to have the class average print, but whenever I try changing my code about, I keep getting this error message:
"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". My code is as follows:

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

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
fngrd = ((0.1*homework)+(0.3*quizzes)+(0.6*tests))
return fngrd

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"

 students = [lloyd,alice,tyler]

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

I commented out the "print average(results)" to test the other functions I wanted to print to the screen. How do I get all these functions to print to the console, and what am I missing?


#2

Hi @bitsolver35607,

We cannot see the indentation because your posted code is not formatted. However, if the following statement is indented in the code that you submitted for this exercise, then it is part of the get_letter_grade function ...

    students = [lloyd,alice,tyler]

The students variable needs to be defined outside all of the functions, so that it is global.


#3

Okay, It appears that no matter how I try to define it globally it appears to fail. I tried to define it before the structs: lloyd,alice, and tyler. Then I tried defining it before the function "def get_average(student)", but I still receive the same error message:"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".


#5

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


#6

In your get_class_average function, you have this in the for loop ...

    results.append(get_average(students))

However, you should be passing student to the get_average function there rather than passing students. The entire group is represented by students, and within the loop, each individual is represented by student.

Other problems may relate to the indentation of various lines in your program, however, since your posted code is not formatted, we cannot see that indentation.

See How do I format code in my posts?, then post a formatted copy of your current code, if the problems persist.


#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!
students = [lloyd,alice,tyler]
def average(numbers):
    total = 0
    total = sum(numbers)
    avg = float(total)/len(numbers)
    return avg
    
def get_average(student):
     homework = average(student["homework"])
     quizzes  = average(student["quizzes"])
     tests    = average(student["tests"])
     fngrd   = ((0.1*homework)+(0.3*quizzes)+(0.6*tests))
     return fngrd

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))

#8

Thank you for the link. This is how my code looks as of now.


#9

Thanks for the formatted code. It reveals that these two final lines in your program are indented, making them part of the get_class_average function ...

    print get_class_average(students)
    print get_letter_grade(get_class_average(students))

They should not be part of the function, therefore the indentation should be removed.


#10

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