List index out of range? Python -


so i'm having trouble coding homework, i'm trying make gpa calculator , think complete part, won't work because keeps telling me there list index out of range in line 8.

def main():     file_name = input("enter filename:  ")     file = open(file_name, "r")     total = [0, 0]     count = [0, 0]     line in file:         sline = line[:-1].split(" ")         grade = sline[1]  # here         units = sline[2]         grade_points = (ord("e") - ord(grade))         print(grade, units, grade_points)     total = units * grade_points     gpa = total / units     print(total)  main() 

in line 8, says sline[2], index # 2. apparently split perform, doesn't produce 3rd index (remember, index's begin on 0, not 1). when split doesn't have 3rd index, you're asking without checking if exists, index out of range. that's why you're getting error.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -