条件语句 Conditionals
根据py4e (python for everybody), 记录python自学过程。习题以及语法可以参考以下链接:
py4e-03-conditional execution
Exerecise 3.1
Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.
hrs = input("Enter Hours:")
fh = float(hrs)
rs = input("Enter Rate:")
fr = float(rs)
if fh>40:
otp = 40 * fr + 1.5 * fr * (fh-40) #otp refers to overtime pay
print(otp)
else:
rgp = fr * fh
print(rgp) # rgp refers to regular pay
Exerecise 3.2
Rewrite your pay program using try and except so that your program handles non-numeric 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
hrs = input("Enter Hours:")
rs = input("Enter Rate:")
try:
fh = float(hrs)
fr = float(rs)
except:
print("Error,please enter numeric input"