Reputation: 23
Here is my code: *Student_IDs is a dictionary
def avg_GPAwork(work_choice):
work = []
work = list((Student_IDs.values()))
print(work)
for hours in range(len(work)):
student_hours = work([hours][6])
print(student_hours)
Here is what work is:
[
['Childs, Julia', '9939456', '12/12/1993', 'Computer Science', '3.89', '8'],
['Gaga, Lady', '7733469', '10/10/1980', 'Computer Science', '1.20', '40'],
['Joe, Billy', '80978632', '01/10/1992', 'Undeclared', '2.4', '25'],
['Armstrong, Lance', '0034728', '07/21/1993', 'Computer Science', '3.47', '10'],
['Cronkite, Walter', '4357349', '10/02/1992', 'Math', '3.21', '0'],
['Williams, Serena', '8453392', '02/14/1994', 'Math', '3.09', '5'],
['Harmond, Josh', '30145692', '12/03/1994', 'Biology', '3.9', '0'],
['Pawlakos, Eric', '1234567', '05/23/1993', 'Statistics', '3.8', '8']
]
I continue to get an indexing out of range error. What am I doing wrong/how can i fix it?
Upvotes: 2
Views: 91
Reputation: 280291
work([hours][6])
I don't know what you think that does, but it doesn't do what you're thinking. You probably wanted the following:
work[hours][5]
The first problem is that your parentheses cause the expression to be parsed as "Call the object work
as if it were a function. The argument should be determined by creating a 1-element list [hours]
, then getting the element at position 6".
The second problem is that indexing starts from 0, so if you want the 6th element, that's at index 5.
Note: range-len iteration is almost never a good idea. If you want to iterate over the elements of a list, do so directly:
for item in work:
student_hours = item[5]
print(student_hours)
or better yet, scrap the first half of the function and iterate over Student_IDs.values()
directly:
for item in Student_IDs.values():
student_hours = item[5]
print(student_hours)
Upvotes: 2