为什么我从这个程序得到错误的结果来计算总薪酬?

时间:2015-06-23 04:09:56

标签: python

这实际上是一项功课。这是问题所在。

  

编写一个程序,使用raw_input计算总薪酬,提示用户每小时和每小时的费率。对于工作时间超过40小时的小时工资小时工资,按小时工资计算为小时工资的40倍和1.5倍。使用45小时和每小时10.50的速度来测试程序(工资应为498.75)。您应该使用raw_input读取字符串,使用float()将字符串转换为数字。不要担心错误检查用户输入 - 假设用户正确输入数字。

我试过这个,但它无法正常工作。

hrs = raw_input("Enter Hours:")
h = float(hrs)
rate_hour = raw_input("Enter rate:")
r = float(rate_hour)
if h <= 40:
    r = 1
else :
    r = 1.5
print h * r 

6 个答案:

答案 0 :(得分:2)

你在做什么 -

  1. 如果该人工作时间超过40小时,则需要支付1.5倍的工作时间。
  2. 如果该人工作时间少于40小时,则需要支付1次工作时间。
  3. 如果小时数小于40,这可以正常工作,但根据您的问题,您需要做的是 -

    1. 如果该人工作时间超过40小时,则需要支付1次,最多40小时,然后在40小时后支付1.5次(即total hours - 40)。

答案 1 :(得分:2)

首先,在用户输入每小时费率后,您将小时费率设置为1和1.5。 其次,你误解了随着时间的推移计算。它应该只适用于超过40小时的工作时间。不是所有时间。 这是对代码的粗略修改。

hrs = raw_input("Enter Hours:")
h = float(hrs)
rate_hour = raw_input("Enter rate:")
r = float(rate_hour)
if h <= 40:
    print h * r
else :
    print (40 * r) + (h -40) * r * 1.5

答案 2 :(得分:1)

您接触此问题的方式存在一些问题。

  1. 该问题规定工资率仅在工作40小时后才会增加,因此如果工作45小时,则只有5小时可以获得加班费。

    查看您编写create or replace FUNCTION FUNC_get_ t_type_end_date (…) return number as -- define a variable to return a number and assign 0 to it -- define a cursor to obtain t_type_price, t_type_end_date of the given t_type_p. The t_type_end_date values should be sorted in descending order – to do so the first record will contain either null or the latest of t_type_end_date BEGIN -- open cursor -- fetch the first record from the cursor to t_type_price_p and t_type_end_date_p -- if (having a record) then … -- close cursor RETURN … END; 语句的方式,您应该能够看到代码与问题之间的不一致。特别是,您需要查看if部分。

  2. 使用标准或加班费率计算工资时,请记住新费率是旧费率的函数。您应该能够在代码中看到设置else:r = 1会导致问题。假设已经工作了10个小时,您的代码将生成r = 1.5而不是10 * 1

  3. 为了帮助你:

    • 要修改变量10 * rate_hour,使其加倍,number
    • 计算工资超过40小时需要进行2次单独计算,因此您可能需要在variable = variable * 2pay下创建变量if:,然后{{1}那个。

答案 3 :(得分:1)

hrs = input("Enter Hours:")

h = float(hrs)

rr=input("enter the rate")

r=float(rr)

if h<=40:

         pay=h*1.5
else:

        pay=(40 * r) + (h -40) * r * 1.5

print(pay)

答案 4 :(得分:0)

hours = input('Enter Hours')
rate = input('Enter Rate')

hours = float(hours)
rate = float(rate)

if (hours) > 40:
    pay = (((hours - 40) * 1.5) * rate) + 40 * rate
    print (pay)

答案 5 :(得分:0)

score=input("Please type a score between 0.0 and 1.0:")

尝试:

float(score)

  if float(score) >= 0.9 and float(score) <= 1.0:

    print("A")

  elif float(score) >= 0.8 and float(score) <= 0.9:

    print("B")

  elif float(score) >= 0.7 and float(score) <= 0.8:

    print("C")

  elif float(score) >= 0.6 and float(score) <= 0.7:

    print("D")

  elif float(score) > 0 and float(score) <= 0.6:

    print("F")

  else:

    print("Bad score.  Please run the program again.")

except:

    print("Bad score.  Please run the program again.")