Average([3, 0]) resulted in an error: ‘int’ object has no attribute ‘getitem‘

average([3, 0]) resulted in an error: ‘int’ object has no attribute ‘getitem’
is an error when i wrote this code in (5.Its Okey to be Average) in last part of learn python 2 , pls whats the problem:

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 subtotal(students):
  total = 0
  for x in students:
   total = sum(x["homework"]) + sum(x["quizzes"]) + sum(x["tests"])
   return total


def length(students):
 for x in students:
   leng = len(x["homework"]) + len(x["quizzes"]) + len(x["tests"])
   return leng


def average(Y):
  average = subtotal(Y) / length(Y)
  return average


for x in students:
 print x["name"]
 print average(students)
 print

There’s a couple of quirks in your code:

This is not going to loop properly for one thing because the return statement will break the loop. The second time you do this with a loop the same problem is going to be relevant.

def subtotal(students):
  total = 0
  for x in students:
   total = sum(x["homework"]) + sum(x["quizzes"]) + sum(x["tests"])
   return total

This line is also a little redundant and perhaps not what you want. This prints the name of every student and then the average of all the students, but it prints the average of all the students for every name… (if the code runs, I’m only looking at this conceptually at the moment).

for x in students:
 print x["name"]
 print average(students)
3 Likes

In addition to @toastedpitabread’s point.

If you look at how your function, average(), is written, it is calling another function subtotal().

subtotal() is written specificly for your dictionaries, and as such uses the keys you have written in those dictionaries. When this is done it uses the built in attribute, __getitem__, which is not part of the int type.

3 Likes

I think conceptually it’s a little ambiguous as to what’s happening, what do you think @8-bit-gaming?

I think the average function acts like the parameter is going to be of a single student, and the functions length and subtotal act like the parameter is going to be a list of students.

I might be inclined to first define functions that deal with getting the average for a single student and then just loop a list over that. That in and of itself can be a get_all_averages function. At least in the beginning, maybe after there can be some refactoring.

1 Like

It is somewhat difficult to tell exactly what is supposed to be happening here.
A link to the lesson would be helpful.

It seems as if average was designed for a single object, though as you said subtotal() and length() both require a list of dictionaries in order to function properly, and the dictionaries must have those specific keys.

Since keys are being used __getitem__ is used, and that is an attribute of dictionaries, if you don’t have a list of dictionaries, than the list indexing fails since it tries to use a key as the index.

1 Like