samedi 6 décembre 2014

Python "list index out of range error"


Vote count:

0




I have written some simple Python code that should select the second element of each list within the main list and provide the user with a sum of all of these values. It should also multiply the second and third elements of each list within the main list and then sum these values.


However, when I attempt to run this code I am confronted with a "list index out of range" error.


How could I avoid this? Thanks



usa_univs = [ ['California Institute of Technology',2175,37704],
['Harvard',19627,39849],
['Massachusetts Institute of Technology',10566,40732],
['Princeton',7802,37000],
['Rice',5879,35551],
['Stanford',19535,40569],
['Yale',11701,40500] ]

def total_enrollment(usa_univs):
a = 0
c = (len(usa_univs)) -1
while c > 0:
a = a + ([c][1])
b = b + (([c][1])*([c][2]))
c = c - 1
print (a,b)


print total_enrollment(usa_univs)


asked 1 min ago







Python "list index out of range error"

Aucun commentaire:

Enregistrer un commentaire