Python Forum

Full Version: Help with homework
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
https://www.chegg.com/homework-help/ques...d=JRwzpHcR

This is my homework question and I am using the code given to me, but I keep getting an error at line 7 saying 'list index out of range' I'm not sure what to do to fix it. Here's my code
import re
f = open("payroll.txt", "r")
print("Name\tID\tGross Pay")
for line in f.readlines():
    fields = re.split(r'\t+', line.strip())
    name = fields[0]
    id_num = int(fields[1])
    pay_type = fields [2]
    pay_rate = float(fields[3])
    if pay_type == 'H':
        hours_worked = int(fields[4])
    if pay_type == 'H':
        gross_pay = pay_rate*hours_worked
    else:
        gross_pay = pay_rate
    print("%s\t%d\t%.2f"%(name, id_num, gross_pay))
f.close()
My guess is that there is a problem with the input file, and some line does not have all the fields expected.