Home > Mobile >  Wrong calculation in Python for overpayment
Wrong calculation in Python for overpayment

Time:10-23

I started an online training for Python. The assignment wants to me calculate 45 hours work for 10.50 hourly payment. But, 5 hours in this is overwork. So, the payment goes to 10.50 X 5 So, the payment should be 498.75.

But, my program finds different sum. What is wrong with this code? (Note that I am novice)

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)

CodePudding user response:

you need to substract extra hours from basic hours on calculations and also add check that extra_pay will not be in negative. Do, Like this:

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)

CodePudding user response:

The problem is that you are adding the overpay hours twice, once with the increased rate in extraPay and once in your payment calc (a is still 45). Also, you need to check if hours are less than 40 because if it is less, extraPay will be negative and your calculation will be wrong.

Here is my suggestion:

def computepay(hours, rate):
    bonus = 0
    if hours>40:
        overtime = hours-40
        hours=40
        bonus = (overtime)*rate*1.5
    return hours*rate bonus

CodePudding user response:

Try:

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)

you used all the hours instead for a max of 40 to calculate the base pay

  • Related