I want somebody to explain what is the difference between these two was of solving this problem and which one would be better.
Rewrite your pay program using try and except so that your program handles non-numerical 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
input_hours = input('Enter Hours: ')
try:
hours = float(input_hours)
except ValueError:
print('Error, please enter numeric input')
quit()
input_rate = input('Enter Rate: ')
try:
rate = float(input_rate)
except ValueError:
print('Error, please enter numeric input')
quit()
if hours < 40:
pay = rate * hours
else:
overtime = hours - 40
pay = (rate * 40.0) + (1.5 * rate * overtime)
print(pay)
or
try:
hrs = input('Enter Hours: ')
hr = float(hrs)
rate = input('Enter Rate: ')
rt = float(rate)
if float(hr) <= 40:
print(hr * rt)
else:
hrr = hr - 40
rr = hrr * 1.5 * rt
print(40 * rt + rr)
except:
print('Error, please enter numeric input')