In: Computer Science
Suppose we have an input file that contains information about how many hours each employee of a company has worked. The file looks like the following (Employee's name, Hours): John 6.5 9.5 7.25 9 8.55 Oscar 12.5 13.5 14 16 18.75 Judith 7 7 7 ••• Construct a program that reads this file and calculates the total number of hours worked by each individual. Make sure that the program handles each specific exception that could occur.'
python
Short Summary:
**************Please do upvote to appreciate our time. Thank you!******************
Source Code:
import sys
# Using readlines()
file1 = open('myfile.txt', 'r')
Lines = file1.readlines()
# Strips the newline character
try:
for line in Lines:
list1 = line.split()
list2 = list1[1:len(list1)]
count=0
for i in list2:
hour=float(i)
count=count+ hour
print("Name:",list1[0],", Hours worked:",count)
except ValueError:
print("Hours are not in number format")
except:
print("Unexpected error:", sys.exc_info()[0])
Code Screenshot:
Output:
**************Please do upvote to appreciate our time.
Thank you!******************