-2

I'm making a program that calculates weekly pay and adds in overtime if a person exceeds 40 hours. The variable shows the correct value but I'm having an issue pushing it back to main to display, and I keep getting a None value when printing

hours = 45
rate = 10
overtime = 12
totalPay = 0




def basePay(hours, rate, totalPay):
    if (hours <= 40):
        totalPay = (totalPay + (hours * overtime))
        print(totalPay)
        return totalPay
   
    else:
        totalPay = (40 * rate)
        hours = (hours - 40)
        basePay(hours, rate, totalPay)

def main():
    print(basePay(hours, rate, totalPay))
    print(hours)

main()

I thought the variable was only available in the function block so I tried making it a global. value.

RK9
  • 1
  • Which variable are you talking about ? There are several prints here. The best would be to show the (complete) output you have. – Itération 122442 Aug 10 '23 at 11:20
  • 3
    You have recursive call to `basePay` without return (in the else branch) – Lesiak Aug 10 '23 at 11:20
  • He means "recursive" ... – Stephen C Aug 10 '23 at 11:21
  • When `hours <= 40` is `false` the `basePay` function never returns anything. Since your initial value for `hours` is `45`, the function doesn't return anything. Perhaps you simply forgot to add a `return` statement in the `else` block? – David Aug 10 '23 at 11:22
  • Thanks, the error was solved after adding a return call to the else statement. I thought the recursion would return "payTotal" through the if statement alone. – RK9 Aug 10 '23 at 11:36

0 Answers0