In: Computer Science
Create a program wages.py that assumes people are paid double time for hours over 60. They get paid for at most 20 hours overtime at 1.5 times the normal rate.
For example, a person working 65 hours with a regular wage of $10 per hour would work at $10 per hour for 40 hours, at 1.5 * $10 for 20 hours of overtime, and 2 * $10 for 5 hours of double time, for a total of
10*40 + 1.5*10*20 + 2*10*5 = $800.
The number of hours should be generated randomly between 35 and 75.
If the number of working hours is less than 40, display message “The salary cannot be generated” with sound of system bell as a warning.
The Python code to randomly generate a number of hours worked betwee 35,75 and to calculate wages according to given condition is:
# Importing random library to use random() function
# Importing winsound to generate a beep sound using winsound.beep()
import random
import winsound
# randomly generating a number in range (35,75)
# using random.randrange(35,75)
hours = random.randrange(35,75)
# Fixing basic wages as 20
wage=20
# Checking if number of hours worked is less than 40
if(hours<40):
print("Number of hours worked: {}".format(hours))
print("The Salary can't be generated")
# Setting frequency, duration to generate warning sound
frequency = 2500
duration = 1000
# Generating beep sound using winsound.beep(frequency,duration) function
winsound.Beep(frequency, duration)
else:
print("Number of hours worked: {}".format(hours))
print("Pay per hour 20\nOvertime 30/hour for less than 60 hours\nOvertime 40/hour for greater than 60 hours")
# If number of hours worked greater than 40
# Cheking all conditions and calculating total wage
if(hours>40):
if hours<=60:
pay= (40*wage)+ (hours-40)*(wage*1.5)
if hours>60:
pay = (40*wage)+ (60-40)*(wage*1.5) + (hours-60)*(wage*2)
print("Your pay: {}".format(pay))
For hous less than 40 "Salaray can't be generated" message is displayed with a beeep sound from system. The sound is produced using winsound.beep(frequency, duration) function found in winsound library.
If the hours greater than 40 then I have calulated wage for first 40 hours $20/hour and 1.5 times 20 i,e $30/hour for 40 to 60 hours and for greater than 60 hours double the wage i.e $40/hour and printed the result.
If you need to change the basic wage from $20 to some other value, you can just change the wage variable in code.
I have tested the code many times and validated the output by checking calculation by hand. The code is working fine. I am sharing few output screenshots for your reference.
Hope this answer helps you.
Thank you :)