EDIT: Have updated post for better clarity, no answers have yet to help!
Alright, so my assignment is to take a text file, that would have 4 entries per line, those bein
I found the duplicate, and think some people treat rude ;/ Just not focus on pragmatic problems of programmers but on good rules of Stack in bad way :(
Here is my complete answer for your problem:
1) First of all, you must remember that ident is used against code block's brackets known from another landuages.
I reformatted your code remember that all of lines should have extra spaces at the beginning when you pase it here ;)
2) like it was said:
first = word.split()
fix "not changing" of lines in loop.
3) Total overtime hours have hardcoded number:
heading6= "{0:15s}{1:16d}".format("Total Overtime Hours", overHours)
Also, overHours(All?) should be not "zeroed" in 'else' block in loop. You must initialize it before loop.
I change some other places i.e. some hardcoded ints, maybe it not ideal and in your style, but you have code with my fixes below...
Best, if you use GitHub or Bitbucket or another repo accesible by web, because you help to contribute if you want it, and also - yourself, to find all the changes which was done. And then, just ask here to help in extremely unknown problems. In the begging of learning it is always hard to find out, but later - you could achieve more!
Here is code after my changes:
from os.path import isfile as isFileExsist
import sys
filePath = input("What is the name of your file?: ")
while isFileExsist(filePath) == False:
pos = ['y', 'Y', 'yes', 'Yes']
neg = ['n', 'N', 'no', 'No']
answer = input("File not found! Do you want to start again? (y-yes/n-no)")
if answer in neg:
exit("Bye!")
elif answer in pos:
filePath = input("What is the name of your file?: ")
continue
else:
print("Not sure what is the answer. Try again!")
continue
file = open(filePath, 'r')
data = file.readlines()
print("{0:15s}{1:15s}{2:10s}{3:15s}{4:20s}{5:15s}".format("First Name", "Last Name", "Hours", "Payrate", "Overtime Hours", "Gross Pay"))
print("==============================================================================================================")
overHoursAll = 0
grossPayAll = 0
count = 0
for line in data:
words = line.split()
lastName = words[0]
firstName = words[1]
initialHours=(int(words[2]))
payRate =(int(words[3]))
if initialHours > 40:
regHours = 40
overHours = initialHours - 40
regPay = payRate * regHours
otPay = overHours * (payRate * 1.5)
grossPay = regPay + otPay
else:
regHours = initialHours
overHours = 0
grossPay = initialHours * payRate
grossPayAll += grossPay
overHoursAll += overHours
# heading3
print("{0:15s}{1:15s}{2:2d}{3:10d}{4:14d}{5:24.2f}".format(firstName, lastName, regHours, payRate, overHours, grossPay))
# space heading
print(" ")
# overall stats
print("{0:15s}{1:21.2f}".format("Total Gross Pay", grossPayAll))
print("{0:15s}{1:19.2f}".format("Average Gross Pay", grossPayAll / len(data)))
print("{0:15s}{1:16d}".format("Total Overtime Hours", overHoursAll))
Best regards, I am sorry for my English.