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