Python Forum
Help with homework - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Help with homework (/thread-16402.html)



Help with homework - alexisnoel13 - Feb-26-2019

https://www.chegg.com/homework-help/questions-and-answers/eureka-lumber-company-would-like-calculate-gross-pay-employees-emplovees-information-store-q35229707?trackid=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()



RE: Help with homework - ichabod801 - Feb-26-2019

My guess is that there is a problem with the input file, and some line does not have all the fields expected.