Here is the assignment prompt:
Write a pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Use try and except so that your program handles non-numeric input gracefully by printing a message and exiting the program. The following shows two executions of the program:
Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input
Enter Hours: forty
Error, please enter numeric input
Here's how I wrote my code:
# Prompts the user to enter the hours and rates
hrs = input('Enter hours: ')
#if hours are non-numeric, give the error message
if hrs is str:
print('Please enter a valid number for the hours or rate!')
#otherwise, proceed with the calculation
else:
try:
rate = input('Enter hourly rate: ')
fh = float(hrs)
fr = float(rate)
if fh > 40:
extra = float(fh - 40)
otpay = (40*fr)+((fr*1.5)*extra)
print('You have overtime! Your pay is: ', otpay)
else:
normpay = fh*fr
print('No overtime! Your pay is: ', normpay)
#if rate is non-numeric, give the error message
except:
print('Please enter a valid number for the hours or rate!')
The problem I am having is that even when hours are in non-numeric form, it doesn't display the error message and moves on to prompting the user to enter their rate. For example:
Enter hours: ten
Enter hourly rate:
I am fairly new to python, and I am not sure what exactly I am doing wrong here