6.1k views
2 votes
Employee Payroll Summary: PYTHON 3 CODE WITHOUT USING ARRAY AND CORRECT INDENTATION WITH ALL VARIABLES DECLARED WINDOWS

For this assignment, you will write a program that reads employee work data from a text file, and then calculates payroll information based on this file. The text file contains one line of text for each employee. Each line consists of the following data (delimited by tabs):

employee’s name
employee’s hourly wage rate
hours worked Monday
hours worked Tuesday
hours worked Wednesday
hours worked Thursday
hours worked Friday

User Ricco
by
6.2k points

1 Answer

4 votes

Answer:

Check the explanation

Step-by-step explanation:

If raw_input() shows some error in Python v3 then call input() instead of raw_input().

def menu():

option = 'n'

while option not in ['r','p','d','h','l','q']:

print("Menu of choices:")

print("\t(r)ead employee data")

print("\t(p)rint employee payroll")

print("\t(d)isplay an employee by name")

print("\tfind (h)ighest paid employee")

print("\tfind (l)owest paid employee")

print("\t(q)uit")

option = input("Please enter your choice: ")

if option not in ['r','p','d','h','q']:

print("Invalid choice. Please try again\\")

return option

def readEmployees(names,wages,hours):

fileRead = 0

try:

filename = input("Enter the file name: ")

f = open(filename,"r")

for line in f:

values = line.split("\t")

names.append(values[0])

wages.append(float(values[1]))

i=0

total = 0

while i < 5:

total = total + float(values[2+i])

i = i+1

hours.append(total)

fileRead = 1

f.close()

except ValueError:

print("Bad data in file " + filename)

except IOError:

print("Error reading file " + filename)

return fileRead

def printPayroll(names,wages,hours):

print("")

print("Name"+" "+"Hours"+" "+"Pay")

print("----"+" "+"-----"+" "+"---")

i = 0

sortedNames = names

sortedNames.sort()

totalpay = 0

for name in sortedNames:

i = names.index(name)

pay = hours[i] * wages[i]

name = names[i]

totalpay = totalpay + pay

name = repr(name).ljust(10)

name = name.replace("'","")

print( name + repr(hours[i]).ljust(9) + repr(pay).ljust(6))

i = i + 1

print("\\Total payroll = $" + str(totalpay))

def displayName(names,wages,hours):

name = ""

i=0

size = len(names)

name = input("Enter the employee's name: ")

while i < size:

if names[i].lower() == name.lower():

wage = wages[i]

hour = hours[i]

break

i = i +1

if i == size:

print("Employee not found")

else:

print(name + " worked " + str(hour) + " hours at $" + str(wage) + " per hour, and earned $" + str(wage * hour) + "\\")

def showHighLow(names,wages,hours, ind):

highestIndex = 0

if ind == 'h':

i =1

size = len(names)

wage = 0;

while i < size:

wage = hours[i] * wages[i]

if wage > (hours[highestIndex] * wages[highestIndex]):

highestIndex = i

i = i + 1

print(names[highestIndex] + " earned $" + str(hours[highestIndex] * wages[highestIndex]) + "\\")

elif ind == 'l':

lowestIndex = 0;

i =1

size = len(names)

wage = 0;

while i < size:

wage = hours[i] * wages[i]

if wage < (hours[lowestIndex] * wages[lowestIndex]):

lowestIndex = i

i = i + 1

print(names[lowestIndex] + " earned $" + str(hours[lowestIndex] * wages[lowestIndex]) + "\\")

def main():

option = 'n'

names = list()

wages = list()

hours = list()

fileRead = 0

while option != 'q':

option = menu()

if option == 'r':

fileRead = readEmployees(names,wages,hours)

elif option == 'p':

if fileRead == 0:

print("Employee data has not been read.")

print("Please read the file before making this choice")

else:

printPayroll(names,wages,hours)

elif option == 'd':

if fileRead == 0:

print("Employee data has not been read.")

print("Please read the file before making this choice")

else:

displayName(names,wages,hours)

elif option == 'h':

if fileRead == 0:

print("Employee data has not been read.")

print("Please read the file before making this choice")

else:

showHighLow(names,wages,hours,'h')

elif option == 'l':

if fileRead == 0:

print("Employee data has not been read.")

print("Please read the file before making this choice")

else:

showHighLow(names,wages,hours,'l')

elif option == 'q':

print("GoodBye")

if __name__ == "__main__":

main()

Kindly check the OUTPUT in the attached image below.

Employee Payroll Summary: PYTHON 3 CODE WITHOUT USING ARRAY AND CORRECT INDENTATION-example-1
User Dhiral Kaniya
by
6.2k points