i am getting this error UnboundLocalError: local variable 'pay' referenced before assignment on line 9 every time please

Viewed 27

#Please help me

def computepay(h, r):
    if h>40:
        reg=40*r
        otp=(h-40.0)*1.5*r
    else:
        pay = h*r
    return pay

hrs = input("Enter Hours:")
rate= input('enter rate per hour: ')
fh = float (hrs)
fr = float (rate)
p = computepay(fh, fr)
print("Pay", p)
2 Answers

If I understand your code correctly, your code is not pasted well, if h > 40 pay is not assigned any value, pay only is assigned a value in the else That's why you get that error

To answer the the question about "pay" being referenced before being assigned, your code is running through the if statement portion of the line where you define computepay(). You never add reg and otp into a variable called pay, which is what I assume you're trying to do.

Also, in your else statement, you make pay = hr, which does not exist, since python will parse hr as one variable and not two, be sure to use asterisks for everything to parse correctly.

How I would type out the computepay() definition would look like this:

def computepay(h, r):
    if h>40:
        reg=40*r
        otp=(h-40.0)*1.5*r
        pay=reg+otp
    else: pay = h*r
    return pay

The main function below return pay is fine, though. This should run perfectly, good luck!

Related