How's everyone doing ? Getting undefined


#1



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


Getting "students" is undefined for some reason ..... what's wrong ???


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"])
    summed = (0.1 * homework) + (0.3 * quizzes) + (0.6 * tests)
    return summed
    
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(lloyd)


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


#2

Just advised someone on this same error a minute ago what a coincidence! :sweat_smile:

You haven't stored lloyd, alice and tyler as a list in the variable students that you are trying to call on this line.

Note: It must be a global variable. Meaning - outside any functions.


#3

Yeah buddy I noticed in a moment I posted my question somebody else posted as well for the same bug lol ! It would be even more funny if that fellow coder was in the same library as I am.

Listen I worked this one out myself but thanks for confirmation. The thing is after fixing this missing global variable I'm getting "Did you remember to print the letter grade for the class's average?"

I think that's what this code calls for

print get_letter_grade(students)

What do you think ?

Here's my updated code in case you wanna see it:

students = [lloyd, alice, tyler]

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"])
summed = (0.1 * homework) + (0.3 * quizzes) + (0.6 * tests)
return summed

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

def get_class_average(students):
result = []
for student in students:

    result.append(get_average(student)) 
return average(result)

print get_class_average(students)
print get_letter_grade(students)


#4

This line poses the final error because notice the excercises says,

Finally, print out the result of calling get_class_average with your students list. Your students should be [lloyd, alice, tyler].
Then, print the result of get_letter_grade for the class's average.

You only called get_letter_grade in your last line instead of also including get_class_average like this,

print get_letter_grade(get_class_average(students)) .


#5

I was banging my head against the wall for this one. Because preview was showing me the result I was looking for but I didn't get "Way to go !"

Dammit I could figure this one out too ...... cheers mate ! I passed .... appreciate your time ....


#6

You're welcome :grin: Feel free to click the solution button below my reply if that was the answer you were looking for. :slight_smile:


#7

Hi,

Below is the code i used for get_letter_grade & get_class_average and get the error message as
"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"

could you please let me know where i am doing wrong

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]
}

classes = [lloyd, alice, tyler]

Add your function below!

def average(numbers):
total = sum(numbers)
x = len(numbers)
total = float(total)
return total / x
print total / x

def get_average(student):
homework =average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])

return 0.10 * homework +  0.3 * quizzes +  0.6 * tests

print get_average(alice)

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:
a = get_average(student)
results.append(a)
return average(results)

print get_class_average(classes)
print get_letter_grade(get_class_average(classes))


#8

Do what I advised @markiscoding to do you need to store lloyd alice and tyler as a list in a variable students. :slight_smile:


#9

Thanks, its working fine now :slight_smile:


#10

You're welcome :slight_smile:


#11

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