条件语句 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"

这篇博客记录了作者在自学Python过程中,按照PY4E课程的学习笔记,包括条件语句、函数、循环与迭代、字符串和文件操作等内容,并提供了相关练习的解答,如工资计算、成绩评级、数值提取等。
最低0.47元/天 解锁文章
1101

被折叠的 条评论
为什么被折叠?



