Could someone please tell me what is wrong with my code?


#1

Could someone please tell me what is wrong with my code? Find the instruction and my solution below. Thanks in advance!

Define a function called get_class_average that has one argument class_list. You can expect class_list to be a list containing your three students.

First, make an empty list called results.
For each student item in the class_list, calculate get_average(student) and then call results.append() with that result.
Finally, return the result of calling average() with results.

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 get_class_average(class_list): #there's an indentation at the beginning of next line
  results = []
  for student in class_list: #there's an indentation at the beginning of next line
    x = get.average(student)
    results.append(x)
    return average(results)

#2

please use format/markup the next time:

so we can actually see the indent.

This is quit useful, given you have an indent issue

A function returns None by default at the end of the function, if we want to return something else at the end of the function, we can use the return keyword, which means the function ends

so, your function ends in the first iteration of the loop, but to get the correct class average with a class of 2 or more students, the loop need to make all its iterations

how could we achieve this?


#3

Thanks for your reply. I think I know what you mean. I now have the return statement outside the for loop. But my code still won’t work when I called get_class_average function. Could you please have a look? Thanks in advance!

The instruction says:

Print out the result of calling get_class_average with your students list. Your students should be [lloyd, alice, tyler].

My code below:

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

class_list = [lloyd, alice, tyler]

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

print get_class_average(class_list)

From Python console
Traceback (most recent call last):
File “python”, line 31, in module
File “python”, line 27, in get_class_average
NameError: global name ‘get’ is not defined


#4

in this exercise you where told to make a list named students, renaming variables is never a wise idea. Name the list students, not class_list

furthermore, your average, get_average and get_letter_grade functions from earlier exercises are missing, while they should be present.


#5

Thanks once again for your reply. There’s currently a bug on codecademy that suddenly moves scripts to an unknown destination. I don’t really know how to explain it but it’s apparently affected many other python learners on codecademy as well. This unexplainable error forced me to reset my exercises otherwise I won’t be able to proceed to the next. Doing this caused the loss of all my previously written codes. It’s a shame that this problem exists because I have had to reset most of my exercises from previous lessons leaving me with nothing. Nevertheless, I didn’t think that the missing scripts should affect the execution of my code.

As per your comment concerning the variable name, the instruction in exercise 8 said we should expect class_list to be a list containing three students. But in the very next exercise (exercise 9), the instruction said to create a variable named students. Again I didn’t think that sticking to the variable name from exercise 8 will pose a problem.


#6

the missing scripts is indeed a serious problem, i thought they fixed it. I will see what i can do about it :slight_smile:

Define a function called get_class_average that has one parameter class_list. You can expect class_list to be a list containing your three students

this means the parameter will be supplied a value of students (the list) as argument on function call


#8

I think that get.average(student) is causing a problem. I do not see “get” anywhere else in your code


#9

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