I ran the following code as a practice purpose from one of the Python book and I want the output as shown in the URL provided below. So, when I am running the first activity in the question (from book - shown in the photo) to verify if he has done it correctly, it's working but when he ran the second activity, it's not working properly. Ideally, I should get except statement printed if I am entering any string in the first user prompted input but I am being asked for the second user prompted input i.e., rate. Why is that so? And also please tell if it is advisable to put complete code in try portion or ideally we should only put the portion in a try statement for which we are sure that it wouldn't work out and it would jump to except block?
When I enter forty, I am not given the error message which is in the book which means that there can be some changes made in the code. However, it is working fine when I am including all lines of codes under try instead of just 2 lines which are currently there i.e., (fh = float(sh) and fr = float(sr)). How can I correct my existing written code by just writing two statements in a try portion?
Your help would be appreciated.
Issue: Enter Hours: fortyError, please enter numeric input
Image of the required output is below:
Following is the code:
sh = input("Enter hours:")
sr = input("Enter Rate:")
try:fh = float(sh)fr = float(sr)
except:print('Error, please enter numeric input')quit()
print(fh, fr)
if fh > 40:reg = fr * fhotp = (fh - 40.0) * (fr * 0.5)xp = reg + otp
else:xp = fh * fr
print("Pay:",xp)