PY4E 课程笔记+练习答案

这篇博客记录了作者在自学Python过程中,按照PY4E课程的学习笔记,包括条件语句、函数、循环与迭代、字符串和文件操作等内容,并提供了相关练习的解答,如工资计算、成绩评级、数值提取等。
摘要由CSDN通过智能技术生成

条件语句 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"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值