1

EDIT: Got it working now, Thanks. It was an empty line on the Text file as someone commented

Beginner here - I'm getting an error saying list index out of range, but I'm referencing a position in the list that should be in range. The error is on the line "courseGPA = GPAconverter(line[2])" which calls my function, GPAconverter.

Im trying to create a program that reads each line of a text file containing a course, the weight of that course, and the grade of that course. I want it to read the course grade, put it through a function i've created, and convert it to a GPA. Then eventually I want the code to output my final GPA.

The format of the txt file is:

Math, 0.5, 80

for line in inputFile:

    line = line.rstrip()
    line = line.split(",")
    courseGPA = GPAconverter(line[2])
    if float(line[1]) == 0.5:
        count = count + 1
        totalGPA += courseGPA
    elif float(line[1]) == 1.0:
        count = count + 2
        totalGPA += 2*(courseGPA)
    elif float(line[1]) == 2.0:
        count += 4
        totalGPA += 4*(courseGPA)
    else:
        print("Somethings wrong")
corey
  • 9
  • 2

2 Answers2

0

It's highly possible that some records/line in textfile is having less than 3 tokens, it would be better to sanitize for the number of tokens before parsing or passing the tokens.

nalamrc
  • 11
  • 2
0

I think this is what you're looking for:

def GPAconverter(param):
    #do something

with open("yourfile.txt") as file:
    inputFile = file.readlines()
    count = 0
    totalGPA = 0
    for line in inputFile:
        line = line.rstrip()
        line = line.split(",")
        courseGPA = GPAconverter(line[2])
        if float(line[1]) == 0.5:
            count = count + 1
            totalGPA += float(courseGPA)
        elif float(line[1]) == 1.0:
            count = count + 2
            totalGPA += 2*float(courseGPA)
        elif float(line[1]) == 2.0:
            count += 4
            totalGPA += 4*float(courseGPA)
        else:
            print("Something is wrong")

print(totalGPA)

Contents of yourfile.txt:

Math, 0.5, 80
Math, 0.5, 80
Math, 0.5, 80
Math, 0.5, 80
Math, 0.5, 80
Math, 0.5, 80
Math, 0.5, 80
Math, 0.5, 80
Math, 0.5, 80
Math, 0.5, 80
...
...

You should be able to replace the contents with whatever you need, but it must have at least three columns, and the second and third elements must be numbers, or it will throw an error, unless you edit the code to work differently.

As @Devesh Kumar Singh said, you might have empty lines in your file. This code should remove them:

import fileinput
for line in fileinput.FileInput("file",inplace=1):
    if line.rstrip():
        print(line)

source: How to delete all blank lines in the file with the help of python?, @ghostdog74

marsnebulasoup
  • 2,530
  • 2
  • 16
  • 37
  • @corey You are welcome. Please consider [accepting my answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if it works for you – marsnebulasoup May 26 '19 at 19:56