我開始了 Python 的在線培訓。任務要我計算 10.50 小時工資的 45 小時作業。但是,這5個小時是加班的。所以,支付去 10.50 X 5 所以,支付應該是 498.75。
但是,我的程式發現了不同的總和。這段代碼有什么問題?(注意我是新手)
hours = float(input("Enter hours: "))
rate = float(input("Enter rate: "))
extraPay = float((hours-40) * (rate*1.5))
def computepay(a, b, c):
paymentCalc= a * b c
return paymentCalc
x = computepay(hours, rate, extraPay)
print(x)
uj5u.com熱心網友回復:
您需要從計算的基本小時數中減去額外小時數,并檢查 extra_pay 不會為負數。做,像這樣:
hours = float(45)
rate = float(10.5)
extra_hours = float(hours - 40)
extraPay = float(extra_hours * (rate*1.5))
def computepay(a, b, c, d):
if c < 0:
c = 0
payment_calc = (a - d) * b c
return payment_calc
x = computepay(hours, rate, extraPay, extra_hours)
print(x)
uj5u.com熱心網友回復:
問題是您添加了兩次超額支付時間,一次是在 extraPay 中增加費率,一次是在您的付款計算中(a 仍然是 45)。此外,您需要檢查小時數是否少于 40,因為如果少于 40,extraPay 將為負數,您的計算將是錯誤的。
這是我的建議:
def computepay(hours, rate):
bonus = 0
if hours>40:
overtime = hours-40
hours=40
bonus = (overtime)*rate*1.5
return hours*rate bonus
uj5u.com熱心網友回復:
嘗試:
hours = float(input("Enter hours: "))
rate = float(input("Enter rate: "))
remainder=(max(0,hours-40))
def computepay(a, b):
paymentCalc= a * b
return paymentCalc
def compute_ext(remainder,rate):
ext = remainder * (rate*1.5)
return ext
base = computepay(min(40,hours), rate)
if remainder:
base =compute_ext(remainder,rate)
print(base)
你用了所有的時間而不是最多 40 來計算基本工資
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/519564.html
標籤:Python功能计算
