Caroline
Caroline

Reputation: 9

My function returns "None"

I am new to Python and I was trying to solve this exercise, but keep getting 'None' output. The question asked for a program in which the input is hours and rate and the output is the gross pay, including overtime if it is more than 40 hours. Anyway, this is the code (I am using Python 3.5.1):

def compute_pay (h,r):
    if h <= 40:
        pay = h*r
        return


    elif h>40:
         pay = (((h-40)*1.5)*r+(40*r))
         return

hours = input ("Enter hours:")
rate= input ("Enter rate")
x = float (hours)
y = float (rate)
p = compute_pay (x,y)
print (p)

Upvotes: 0

Views: 84

Answers (3)

alecxe
alecxe

Reputation: 473863

My function returns “None”

You function does not return anything. You meant to return pay:

def compute_pay(h, r):
    if h <= 40:
        pay = h*r
    elif h > 40:
        pay = (((h-40)*1.5)*r+(40*r))

    return pay

And I think you may shorten your code using the ternary if/else:

def compute_pay(h, r):
    return h * r if h <= 40 else (((h - 40) * 1.5) * r + (40 * r))

Upvotes: 1

Ilya V. Schurov
Ilya V. Schurov

Reputation: 8047

You have to specify what to return in the return statement:

def double_it(x):
    return x*2

Note that x*2 after the return statement.

Upvotes: 1

Greg
Greg

Reputation: 10342

return will return None if you don't give it anything else. Try return pay

Upvotes: 3

Related Questions