当我在另一个函数中调用一个函数时,return 值没有被打印出来,所以我需要使用 print 函数

When I call a function inside another function, the return value doesn't get printed out, so I need to use the print function

我创建了这两个函数 computepay() 和 calculateOverTime。我在 computepay 中调用了 calculateOverTime,计算并返回了它。我希望通过 print(computepay(floatHours, floatRate) 打印出 calculateOverTime 的结果数学。我做错了什么?

def calculateOverTime(floatHours, floatRate):
    overtimeHours = floatHours - 40
    regularPay = (floatHours-overtimeHours)*floatRate
    overtimeRate = floatRate * 1.5
    overtimePay = overtimeHours*overtimeRate
    overtimePayment = overtimePay+regularPay
    return overtimePayment

def computepay(floatHours, floatRate):
if floatRate<=40:
    if floatHours>40:
        calculateOverTime(floatHours, floatRate)
    else:
            regularPay = floatHours*floatRate
            return regularPay
    else:
        print("I can't process this shit")

try:
    floatHours = input("Enter the hours:")
    floatHours = float(floatHours)
    floatRate = input("Enter the rate:")
    floatRate = float(floatRate)

except:
    print("Wrong Inputs")
    input("Try again")

print(computepay(floatHours, floatRate))

input("Close please")

您在调用 calculateOverTime 方法时错过了 return。

return calculateOverTime(floatHours, floatRate)
def computepay(floatHours, floatRate):
    if floatRate<=40:
        if floatHours > 40:
            amount=calculateOverTime(floatHours, floatRate)
            return amount

        else:
            regularPay = floatHours * floatRate
            return regularPay
    else:
        print("I can't process this shit")

你错过了 return calculateOverTime(floatHours, floatRate) ,我接受了可变数量然后 return 它。您也可以直接使用 return calculateOverTime(floatHours, floatRate)

我检查了你的代码并让它工作。

您在 compute_pay 函数中缺少 return calculateOverTime 函数,因此不要只调用该函数,而是在开头添加一个 return :

return calculateOverTime(floatHours, floatRate)