-1
hours = input("Enter hours: ")

rate = input("Enter rate: ")

try:
    hours = float(hours)
    rate = float(rate)
except:
    print("Enter real numbers")

def computepay(hours, rate):
    if hours <= 40.0:
        pay = hours * rate
        return pay
    elif hours > 40:
        pay = 40 * rate
        exhrs = (hours - 40) * (1.5 * rate)
        totpay = pay + exhrs
        return totpay

print("Pay: %s" % computepay(hours, rate))
Gerges
  • 6,269
  • 2
  • 22
  • 44
Che
  • 1

2 Answers2

1

The except block is executed when an exception is raised by the code in the try block. There is no magic involved that would know that your user needs to re-enter those numbers, you'll have to provide the code for that yourself.

Also, you should always be specific with your exceptions. One possible way of doing this would be

while True:
    hours = input("Enter hours: ")
    rate = input("Enter rate: ")
    try:
        hours = float(hours)
        rate = float(rate)
        break              # end the while loop if no error occurred
    except ValueError:
        print("Enter real numbers")
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

It's normal behavior. If you rise some exception then script will be terminated, in other case except will only print message. except doesn't terminate script.

darvark
  • 314
  • 3
  • 15