I am new to Python. For my program, I want to display a message when the user enters a wrong input, such as characters and negative numbers, and get the user to provide input again. I know how to do that using try and except for the exception of character inputs, but I am still figuring out how to use it for negative number inputs. Or is there a better way to do it?
def CalPay(hrs,rate):
print('Please enter number of hours worked for this week:', hrs)
print('What is hourly rate?', rate)
try:
hrs = float(hrs)
hrs>=0
except:
print('You entered wrong information for hours.')
while True:
try:
hrs=float(input('Please enter number of hours worked for this week:'))
except:
print('You entered wrong information for hours.')
continue
else:
break
try:
rate = float(rate)
rate>=0
except:
print('You entered wrong rate information.')
while True:
try:
rate=float(input('What is hourly rate?'))
except:
print('You entered wrong rate information.')
continue
else:
break
if hrs > 60:
pay=((hrs-60)*2*rate)+(20*rate*1.5)+(rate*40)
print('Your pay for this week is:', '$'+str(pay))
elif hrs > 40:
pay=((hrs-40)*1.5*rate)+(rate*40)
print('Your pay for this week is:', '$'+str(pay))
else:
pay=rate*hrs
print('Your pay for this week is:', '$'+str(pay))