In: Computer Science
Write a function pieces_produced takes a number that represents a starting hour between 6am and 6pm, in twenty-four hour format (between 0 and 23), along with the number of workers and returns a number that represents the total number of pieces produced during that hour. If the user enters an invalid hour or number of workers, the function should return -1. Save the function in a PyDev library module named functions.py
Write a testing program named t01.py that tests the function by asking the user to enter hours and number of workers and prints the total number of pieces produced.
A sample run:
Time of the day: 6 Number of workers: 10
The total number of pieces produced:300
Or
Time of the day: 35 Number of workers: 10
Can not perform the calculation
Below is the code in Python 3 for the requirements mentioned in the question. In our program below we have assumed that the starting point is INCLUSIVE in the range but finish point is EXCLUSIVE which means that for range 6 am to 10 am, 6 am is in the range and 10 am is not. This we have to do otherwise we cannot make a decision and we made this decision because as in the example they have given time = 6am and performed caculation for that, which means it is inlusive. Sample text file as mentioned in the question is attached below. Also attached screenshots of three examples different from the examples in the question.
Note:- We have used time as invalid if it is outside the range mentioned in the question(6am to 6pm, where 6pm is exclusive) and number of workers is invalid if they are less than or equal to zero.
# function to return the pieces produced for valid time and workers, otherwise return -1.
def pieces_produced(hour, workers):
pieces = 0
if(workers > 0):
if(hour >= 6 and hour < 10):
pieces = workers * 30
elif(hour >= 10 and hour < 14):
pieces = workers * 40
elif(hour >= 14 and hour < 18):
pieces = workers * 35
else:
return -1
else:
return -1
return pieces
time = input("Enter time of the day: ")
worker_cnt = input("Enter number of workers: ")
# calling function to calculate pieces produced for a given range.
result = pieces_produced(int(time), int(worker_cnt))
# opening file in append mode which means that result will be appended to existing file.
outF = open("testing.txt", "a")
# below condition will get hit if either time or number of workers is invalid.
if(result == -1):
outF.write("Can not perform the calculation for time = {} and workers = {}".format(time, worker_cnt))
outF.write("\n")
print("Can not perform the calculation")
# below condition will get hit if both inputs are valid.
else:
outF.write("The total number of pieces produced for time = {} and workers = {}: {}".format(time, worker_cnt, result))
outF.write("\n")
print("The total number of pieces produced: {}".format(result))
Sample File:-
We have created a file in such a way that every time you write to the file, new result will get written to the existing file in the new line. For that we have open the file in "a" mode , which is append mode.
Sample Output 1:-
Since 15 is in the range 2 pm to 6 pm, total pieces that could be produced = 35 * 15 = 525 {because for this range output is 35 pieces/hour/worker} and workers equals 15.
Sample Output 2:-
Time of the day is invalid as it is not in any of the ranges mentioned in the question.
Sample Output 3:-
Since 15 is in the range 10 am to 2 pm, total pieces that could be produced = 40 * 30 = 1200 {because for this range output is 40 pieces/hour/worker} and workers equals 30.