I need to write a try/except
component with Python 3 to compute the total pay by multiplying wage rate with working hours. The inputs have to be numeric, otherwise print an error message and stop.
hours = input("Enter Hours:")
try:
hours = float(hours)
rate = input("Enter Rate:")
try:
rate = float(rate)
if hours > 40:
hours = hours - 40
print("Pay:", 40*rate + hours*1.5*rate)
else:
print("Pay:", rate*hours)
except:
print("Error. Please enter numeric inputs.")
except:
print("Error. Please enter numeric inputs.")
The codes above just keep running without prompting anything (not even the 1st line). And the below still prompts for the second input after the first fails and the whole thing should stop.
hours = input("Enter Hours:")
try:
hours = float(hours)
except:
print("Error. Please enter numeric inputs.")
rate = input("Enter Rate:")
try:
rate = float(rate)
if hours > 40:
hours = hours - 40
print("Pay:", 40*rate + hours*1.5*rate)
else:
print("Pay:", rate*hours)
except:
print("Error. Please enter numeric inputs.")
I'm a newbie in Python, but I kinda sense there's a simple solution to this. Can somebody help me get that? Thank you very much!