Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with homework
#1
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()
Reply
#2
My guess is that there is a problem with the input file, and some line does not have all the fields expected.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020