0

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)
Caroline
  • 9
  • 1

3 Answers3

3

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

Greg
  • 9,963
  • 5
  • 43
  • 46
1

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))
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

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.

Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78