【Python for Everybody(Getting Started with Python)】Week 6 | Chapter 4 题目汇总

PY4E课程官网:https://www.py4e.com/

参考文章:题目及答案汇总(Github)












Assignment 4.6

Write a program to prompt the user for hours and rate per hour using raw_input to compute gross pay.   
Award time-and-a-half for the hourly rate for all hours worked above 40 hours.   
Put the logic to do the computation of time-and-a-half in a function called computepay() and use the function to do the computation.   
The function should return a value.   
Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75).  
You should use raw_input to read a string and float() to convert the string to a number.   
Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly.  

编写一个程序,提示用户使用raw_input来计算总工资的小时数和每小时工资率。  
对于工作时间超过40小时的所有小时,按小时费率给予时间和半的奖励。  
把计算time and a half的逻辑放在一个名为computepay()的函数中,并使用该函数进行计算。  
函数应该返回一个值。  
使用45小时和每小时10.50的工资率来测试程序(工资应该是498.75)。 
你应该使用raw_input来读取一个字符串,并使用float()将字符串转换为一个数字。  
不要担心用户输入的错误检查,除非你想这样做--你可以假设用户正确地输入了数字。 

# Compute Pay Function
def computepay(hours,rate):
    if hours <= 40:
        pay = hours * rate
    elif hours > 40:
        pay = 40*rate + (hours-40)*rate*1.5
    return pay
# Users input   
input_hours = input("Enter Hours:")
input_rate = input("Enter Rate:")
hours = float(input_hours)
rate = float(input_rate) 
# Output result
print("Pay:",computepay(hours,rate))

>>>Enter Hours:42
>>>Enter Rate:1.2
Pay: 51.6

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值